简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM