简体   繁体   English

使用$ .ajax和C#的JSON格式

[英]JSON format using $.ajax and C#

If I use $.ajax JQuery and I call WebMethod, I get JSON: 如果我使用$ .ajax JQuery并调用WebMethod,则会得到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. 但JSON具有“ d”属性。

function RenderCourses(data) {

    if (data.d.length > 0) {

If I use json = JsonConvert.SerializeObject in C#, hasn't the "d" property. 如果我在C#中使用json = JsonConvert.SerializeObject ,则没有“ d”属性。

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

And RenderCourses fails. RenderCourses失败了。

Any reasons? 有什么原因吗?

ADO.NET WebMethods always serialize the response like this. ADO.NET WebMethods总是像这样序列化响应。 d meand "data". d表示“数据”。 And you can't do anything with this. 而且您对此无能为力。

JsonConvert.SerializeObject is a method from third party software ( Newtonsoft ). JsonConvert.SerializeObject是来自第三方软件( Newtonsoft )的方法。 It just simple serialize your object to JSON. 它只是简单地将您的对象序列化为JSON。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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