Removing the “d” object from ASP.Net web service json output

前端 未结 3 632
天涯浪人
天涯浪人 2020-12-17 03:37

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 service

相关标签:
3条回答
  • 2020-12-17 04:16

    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);
        }
    
    0 讨论(0)
  • 2020-12-17 04:22

    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.

    0 讨论(0)
  • 2020-12-17 04:35

    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 讨论(0)
提交回复
热议问题