Kiquenet
Kiquenet

Reputation: 14976

JSON format using $.ajax and C#

If I use $.ajax JQuery and I call WebMethod, I get JSON:

$.ajax({
    type: "POST",
    dataType: "json",
    data: JSON.stringify({ id: idX, id2: idY }),
    async: true,
    cache: false,
    url: "/ws/Courses.asmx/GetCourses",
    contentType: "application/json; charset=utf-8",
    success: function (data) {
        RenderCourses(data.d);
    },
});

but JSON has "d" property.

function RenderCourses(data) {

    if (data.d.length > 0) {

If I use json = JsonConvert.SerializeObject in C#, hasn't the "d" property.

string script = "var data = " + json + "; RenderCourses(data);";
ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "dataVar", script, true);

And RenderCourses fails.

Any reasons?

Upvotes: 0

Views: 64

Answers (1)

aleha_84
aleha_84

Reputation: 8539

ADO.NET WebMethods always serialize the response like this. d meand "data". And you can't do anything with this.

JsonConvert.SerializeObject is a method from third party software (Newtonsoft). It just simple serialize your object to JSON.

Upvotes: 1

Related Questions