ResponseFormat.Json返回XMLResponseFormat、Json、XML

2023-09-03 01:34:37 作者:蕝蝂ヽ尛爹

我知道即时通讯不是第一次这个问题,但我似乎无法找到一个有效的解决方案。

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

使用Web服务集合,返回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

我试图加入各种HttpHandler的设置,以我的web.config,如在某些职位建议,但这些都没有效果。此外,我不认为它的​​必要,因为即时通讯工作的一个全新的win7 / IIS7.5 / .NET4框。我读了,因为.NET 3.5,不应该有任何问题。但有! 在我试图使用和不使用responseformat.json装饰。我的web服务返回有效JSON(我可以parsejson解析它,解压后的字符串) 在我试图明确设置的contentType和数据类型。这将导致一个错误抱怨的反应是无效的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

真诚

编辑: 继承人,我只是测试的样本Web服务:

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://本地主机: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和 - JSON-共错误和 - 误解/ 这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 :

将内容类型设置为应用程序/ 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.