简体   繁体   English

从ASP.Net Web服务json输出中删除“d”对象

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

I have some javascript code that processes json output from asp.net web services built with framework 2.0. 我有一些javascript代码,用于处理使用framework 2.0构建的asp.net Web服务的json输出。 Now I need to support data returned from framework 3.5 web services. 现在我需要支持从框架3.5 Web服务返回的数据。

My javascript code assumes a list of objects as return value, which works fine in 2.0. 我的javascript代码假设一个对象列表作为返回值,在2.0中工作正常。 However In framework 3.5, the list is wrapped in a "d" object. 但是在框架3.5中,列表包含在“d”对象中。 Is there any way I can remove the "d" wrapper and just return the list? 有什么办法可以删除“d”包装并返回列表吗?

I would prefer to fix this onthe server side 我宁愿在服务器端解决这个问题

You can't configure 3.5+ services not to return the .d . 您不能配置3.5+服务不返回.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. 它也很好,因为它可以防止当外部JSON实体是一个数组时存在的棘手的JSON劫持场景。

ASP.NET AJAX's client-side proxies automatically hide the .d from you. ASP.NET AJAX的客户端代理会自动隐藏.d If it's getting in your way, I'm assuming you're using something like jQuery to call the service? 如果它妨碍了你,我假设你正在使用类似jQuery的东西来调用服务? You can normalize the .d in jQuery by using its DataFilter callback , for example. 例如,您可以使用其DataFilter回调来规范化jQuery中的.d

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

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. 好吧,如果你有改变客户端的优势,那么最好的方法是使用jquery,你会发现很多解决方案。 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). 但是如果要在服务层上删除“d”,最好的方法是在Web Api中重写您的Web服务(您也可以使用WCF)。 Web Api does not return "d" in response. Web Api不会返回“d”作为响应。

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

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