最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Removing the "d" object from ASP.Net web service json output - Stack Overflow

programmeradmin0浏览0评论

I have some javascript code that processes json output from asp web services built with framework 2.0. Now I need to support data returned from framework 3.5 web services.

My javascript code assumes a list of objects as return value, which works fine in 2.0. However In framework 3.5, the list is wrapped in a "d" object. Is there any way I can remove the "d" wrapper and just return the list?

I would prefer to fix this onthe server side

I have some javascript code that processes json output from asp.net web services built with framework 2.0. Now I need to support data returned from framework 3.5 web services.

My javascript code assumes a list of objects as return value, which works fine in 2.0. However In framework 3.5, the list is wrapped in a "d" object. Is there any way I can remove the "d" wrapper and just return the list?

I would prefer to fix this onthe server side

Share Improve this question asked May 11, 2010 at 14:27 MidhatMidhat 17.8k22 gold badges91 silver badges117 bronze badges 2
  • 3 Side note: the d wrapper is a security measure. See encosia.com/2009/02/10/… – Crescent Fresh Commented May 11, 2010 at 14:34
  • related: stackoverflow.com/questions/830112/what-does-d-in-json-mean – Ruben Bartelink Commented Sep 21, 2012 at 1:44
Add a comment  | 

3 Answers 3

Reset to default 7

Here is a way around that

    [WebMethod]
    [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
    public void Status()
    {
        MyObject myObject = new MyObject(); // your object here
        var json = Newtonsoft.Json.JsonConvert.SerializeObject(myObject);

        HttpContext.Current.Response.Write(json);
    }

You can't configure 3.5+ services not to return the .d. It's good that it's there too, because it protects from a tricky JSON hijacking scenario that exists when the outer JSON entity is an array.

ASP.NET AJAX's client-side proxies automatically hide the .d from you. If it's getting in your way, I'm assuming you're using something like jQuery to call the service? You can normalize the .d in jQuery by using its DataFilter callback, for example.

Well if you have the advantage of changing on the client side then best way is using jquery and you will find a ton of solutions. But if you want to remove "d" on service layer the best way is rewrite your webservice in Web Api(You can use WCF also). Web Api does not return "d" in response.

发布评论

评论列表(0)

  1. 暂无评论