简体   繁体   English

AJAX Return对象包含asp.net web表单中的d

[英]AJAX Return object contains d in asp.net web form

Hey guys Its a simple question but want to stop ajax return object contains d . 嘿伙计这是一个简单的问题,但想要停止ajax返回对象包含d

I asp.net web form 我是asp.net网页表单

    $.ajax({
        type: "POST",
        url: myurl,
        data: JSON.stringify({value:"1"}),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (result) {
            //alert("i am in success");
            console.log(result);

        },
        failure: function (response) {
            console.log("I am in failure");
        }
    });

and in .cs file 并在.cs文件中

        [WebMethod]

        public static string getSpareParts(string value)
        {
            List<data> spareParts;
            using (var db = new WaltonCrmEntities())
            {
                spareParts = db.SpareParts.Select(x => new data
                {
                    id = x.ItemID,
                    text = x.ItemName
                }).ToList();
            }
            JavaScriptSerializer js = new JavaScriptSerializer();
            return js.Serialize(spareParts);
            //return spareParts;

        }

when result is return it will contain result in result.d but i want to stop this d thing .Simply result should be the result . 当结果返回它将包含结果result.d ,但我想停止这种d件事.Simply结果应该是result Can anybody solve this. 任何人都可以解决这个问题

There's not much you could do about this. 你无能为力。 That's the way ASP.NET WebMethods always serialize the response. 这就是ASP.NET WebMethods总是序列化响应的方式。 The reason why Microsoft decided to wrap the response in this .d property is to protect against this particular JSON hijacking vulnerability . Microsoft决定在此.d属性中包装响应的原因是为了防止此特定的JSON hijacking vulnerability Of course you could always check in your success callback whether this property is present or not: 当然,您可以随时检查您的success回调是否存在此属性:

success: function (result) {
    var data = result.hasOwnProperty('d') ? result.d : result;
    console.log(data);
}

Also I would very strongly recommend against writing plumbing code in your WebMethod (like serialization stuff) and just return the proper object and let the infrastructure care about serialization: 此外,我强烈建议不要在WebMethod中编写管道代码(如序列化内容),只需返回正确的对象并让基础结构关注序列化:

[WebMethod]
public static List<data> getSpareParts(string value)
{
    using (var db = new WaltonCrmEntities())
    {
        return db.SpareParts.Select(x => new data
        {
            id = x.ItemID,
            text = x.ItemName
        }).ToList();
    }
}

Now inside your success callback you can directly work with this collection without the need to additionally JSON.parse it: 现在在成功回调中,您可以直接使用此集合,而无需另外JSON.parse它:

success: function (result) {
    var data = result.hasOwnProperty('d') ? result.d : result;
    for (var i = 0; i < data.length; i++) {
        console.log('id: ' + data[i].id + ', text: ' + data[i].text);
    }
}

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

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