0

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?

2
  • Try data: { id: idX, id2: idY }, in $.ajax Commented Feb 1, 2018 at 13:14
  • JSON.stringify is the problem? Commented Feb 1, 2018 at 13:16

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.