0

I'm fairly new to using ajax with .net and also with using JSONs. The ajax seems to be encapsulating my JSON into another JSON.

Here is the ajax:

function updateChecks() {
    console.log("updateChecks() called");
    var Sunday = document.getElementById("Sundate").value;
    var Saturday = document.getElementById("Satdate").value;
    var jsonDates = JSON.stringify({ sundate: Sunday, satdate: Saturday });
    console.log(jsonDates);
    $.ajax(
    {
        method: 'Post',
        url: 'WPForm.aspx/getTheDates',
        data: jsonDates,
        dataType: "json",
        contentType: "application/JSON; charset=utf-8",
        success: function (result) { console.log(result); checkCheckedDates(result); },
        error: function (xhr, textStatus, errorThrown) { console.log(textStatus + " " + errorThrown); }
    }
)
};

And here is the c#

    [WebMethod]
    public static string getTheDates(string sundate, string satdate)
    {
        System.Diagnostics.Debug.WriteLine("GET THE DATES " + sundate + " " + satdate);

        WPUser currUser = new WPUser();
        SqlConnection wellnessProgramDB = new SqlConnection(ConfigurationManager.ConnectionStrings["WellnessProgram"].ConnectionString);

        int uID = currUser.getUID((string)HttpContext.Current.Session["username"]);

        string getDatesSQL = "select [Dates Checked] from WP_DatesChecked where [Employee ID] = '" + uID + "' and [Dates Checked] between '" + sundate + "' and '" + satdate + "';";
        SqlCommand getDates = new SqlCommand(getDatesSQL, wellnessProgramDB);

        wellnessProgramDB.Open();

        SqlDataReader dateReader = getDates.ExecuteReader();

        System.Data.DataTable dateTable = new System.Data.DataTable();
        dateTable.Load(dateReader);

        List<string> values = new List<string>();
        foreach (System.Data.DataRow row in dateTable.Rows)
        {
            object value = row["Dates Checked"];
            values.Add(value.ToString());
            System.Diagnostics.Debug.WriteLine("Value: " + value.ToString());
        }

        wellnessProgramDB.Close();

        string datesJson = JsonConvert.SerializeObject(values);

        System.Diagnostics.Debug.WriteLine("JSON: " + datesJson);

        return datesJson;

    }

When I log the JSON before sending it from the server, the JSON is correct. I get JSON: ["3/26/2017 12:00:00 AM","3/28/2017 12:00:00 AM","3/30/2017 12:00:00 AM"].

But when I log it after retrieving it, I get: Object { d: "["3/26/2017 12:00:00 AM","3/28/2017…" }.

I could be missing something minor, but any help is much appreciated!

  • This is standard. You'll probably want `console.log(result.d); checkCheckedDates(result.d);`, or even `result = JSON.parse(result.d)` - You can see a similar question here: [What does .d in JSON mean?](http://stackoverflow.com/a/24065295/2026606) – Tyler Roper Apr 24 '17 at 19:56
  • Yup. That worked. Thanks so much! – Chase Tennant Apr 24 '17 at 20:02

0 Answers0