AJAX Return对象包含asp.net Web表单中的d [英] AJAX Return object contains d in asp.net web form

查看:75
本文介绍了AJAX Return对象包含asp.net Web表单中的d的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,这是一个简单的问题,但要停止ajax返回对象包含 d .

Hey guys Its a simple question but want to stop ajax return object contains d.

我的asp.net网络表单

I asp.net web form

    $.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");
        }
    });

和在.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;

        }

当结果返回时,它将在 result.d 中包含结果,但是我想停止此 d 事情.结果应该只是 result .谁能解决这个问题.

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. Can anybody solve this.

推荐答案

您对此无能为力.这就是ASP.NET WebMethods始终序列化响应的方式.Microsoft之所以决定将响应包装在此 .d 属性中的原因是为了防止此特定的

There's not much you could do about this. That's the way ASP.NET WebMethods always serialize the response. The reason why Microsoft decided to wrap the response in this .d property is to protect against this particular JSON hijacking vulnerability. Of course you could always check in your success callback whether this property is present or not:

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

我也强烈建议您不要在WebMethod中编写管道代码(例如序列化的东西),而只是返回适当的对象,并让基础结构关心序列化:

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]
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();
    }
}

现在,在成功回调中,您可以直接使用此集合,而无需另外 JSON.parse :

Now inside your success callback you can directly work with this collection without the need to additionally JSON.parse it:

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);
    }
}

这篇关于AJAX Return对象包含asp.net Web表单中的d的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆