Removing the "d" object from ASP.Net web service json output
Asked Answered
Q

3

8

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

Quincentenary answered 11/5, 2010 at 14:27 Comment(2)
Side note: the d wrapper is a security measure. See encosia.com/2009/02/10/…Ruthenian
related: https://mcmap.net/q/346760/-what-does-d-in-json-meanGulosity
B
6

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.

Bonefish answered 11/5, 2010 at 16:46 Comment(1)
thanks. I use mootools. Will find something like this for mootools or roll my ownQuincentenary
S
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);
    }
Spleeny answered 5/11, 2018 at 20:54 Comment(1)
So obvious once I saw your answer. I was still getting the d json object after my json response object so I added ...Response.Flush() and ...Response.End() to get rid of it entirely. Thank you.Foxtrot
B
6

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.

Bonefish answered 11/5, 2010 at 16:46 Comment(1)
thanks. I use mootools. Will find something like this for mootools or roll my ownQuincentenary
V
1

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.

Viosterol answered 11/2, 2016 at 7:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.