ResponseFormat.Json 返回 xml [英] ResponseFormat.Json returns xml

查看:23
本文介绍了ResponseFormat.Json 返回 xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我不是第一个遇到这个问题的人,但我似乎找不到可行的解决方案

i know im not the first with this problem, but i cant seem to find a working solution

当使用设置为返回 json 的网络服务时,.net 仍将其包装在 XML 包装器中.

when using a webservice set to return json, .net still wraps it in an XML wrapper.

我搜索并尝试了很多东西

i searched and tried many things

  1. 我尝试按照某些帖子中的建议将各种 httphandler 设置添加到我的 web.config 中,但这些都没有效果.我也认为没有必要,因为我正在开发一个全新的 win7/iis7.5/.net4 框.我读到,因为 .net 3.5 应该没有任何问题.但有!
  2. 我尝试使用和不使用 responseformat.json 装饰.我的网络服务返回有效的 json(我可以在提取字符串后使用 parsejson 解析它)
  3. 我尝试明确设置内容类型和数据类型.这会导致错误,抱怨响应无效 json.这是对的!

发生的事情非常令人困惑,在 IE 中,至少在我的 devbox 中,响应作为 xml 文档返回,我可以在其中使用 msg.text 并轻松获取 json 字符串,但在生产中我在 FF 中进行了测试,它作为文档返回,没有文本"属性.

what is happening is very confusing, in IE , at least on my devbox, the response returns as an xml document where i can just use msg.text and easily get the json string, but in production i tested in FF and it returns as a document, with no "text" property.

这是我的 javascript/jquery

heres my javascript/jquery

$.ajax({
error: function (xhr, ajaxOptions, thrownError) {
    alert(xhr.status+'-'+xhr.statusText);
    alert(thrownError);
    },
    url: '<%=ResolveUrl("~/WebService.asmx")%>' + "/JackJill",
    contentType: "application/json",
    success: function (msg) {
        alert(msg.d); 
    }
});

所以:我怎么能简单地要求 .net 返回一个有效的常规 json 字符串,而不是包装它.我相信这将解决所有问题.它还将使全世界更容易访问我的服务,因此他们无需进行任何特殊解析.

so: how can i simply ask .net to return a valid regular json string, instead of wrapping it. i believe that will solve all the problems. it will also make my service more accessible to the world at large, so they don't have to do any special parsing.

非常感谢您的建议或指点

thank you very much for any advice or pointers

真诚的

这是我刚刚测试的示例网络服务:

heres a sample webservice that i just tested:

<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> <WebMethod()> Public Function JackJill() As String
    Return "[{""Name"":""Jack""},{""Name"":""Jill""}]"
End Function

然后当我把它放在浏览器中

then when i put this in the browser

http://localhost:81/webservice.asmx/JackJill

我明白

<?xml version="1.0" encoding="utf-8" ?>
<string xmlns="http://tempuri.org/">
[{"Name":"Jack"},{"Name":"Jill"}]
</string> 

为什么这里所有这些 xml 的东西?

why all this xml stuff here?

推荐答案

我意识到这个问题很老了,但以防万一其他人将来看到这个答案......

I realise this question is old but just in case someone else is looking at this answer in the future ....

我发现(根据这个http://encosia.com/asmx-and-json-common-mistakes-and-misconceptions/ 和这个 http://weblogs.asp.net/scottgu/archive/2007/04/04/json-hijacking-and-how-asp-net-ajax-1-0-mitigates-these-attacks.aspx),如果您希望从 .ASMX 使用 JSON,则有必要:

I have found (according to this http://encosia.com/asmx-and-json-common-mistakes-and-misconceptions/ and this http://weblogs.asp.net/scottgu/archive/2007/04/04/json-hijacking-and-how-asp-net-ajax-1-0-mitigates-these-attacks.aspx) that if you wish to consume JSON from an .ASMX it's necessary to :

  • 将内容类型设置为application/json"和
  • 将方法设置为POST"

上面的 Yisman 说他不想将他的服务限制为 POST 请求,但是(根据这两个参考)似乎确实如果您希望使用 .ASMX 并接收 JSON,那么您将不得不这样做做.

Up above Yisman says he doesn't want to restrict his service to POST requests but (according to those two references) it does seem that if you wish to use .ASMX and receive JSON that's what you're going to have to do.

这篇关于ResponseFormat.Json 返回 xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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