ResponseFormat.Json 返回 xml

发布于 2024-10-31 04:09:55 字数 1634 浏览 1 评论 0原文

我似乎找不到有效的解决方案

我知道我不是第一个遇到这个问题的人,但在使用设置为返回 json 的 Web 服务时,

,.net 仍然将其包装在 XML 包装器中。我搜索并尝试了很多东西,

  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 中测试,它作为文档,没有“文本”属性。

这是我的 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 字符串,而不是包装它。我相信这会解决所有问题。它还将使我的服务更容易被全世界使用,因此他们不必进行任何特殊的解析。

非常感谢您

真诚的

任何建议或指示编辑: 这是我刚刚测试的示例 Web 服务:

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

然后当我将其放入浏览器时

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

我明白

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

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

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

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

i searched and tried many things

  1. i tried adding various httphandler settings to my web.config, as suggested in certain posts, but these had no effect. also i don't think its necessary as im working on a brand new win7/iis7.5/.net4 box. i read that since .net 3.5 there shouldn't be any problem. but there is!
  2. i tried with and without the responseformat.json decoration. my webservice returns valid json ( i can parse it with parsejson, after extracting the string)
  3. i tried explicitly setting the contenttype and datatype. this causes an error complaining that the response was invalid json. which is right!

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.

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

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

sincerely

EDIT:
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

i get

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

why all this xml stuff here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

可是我不能没有你 2024-11-07 04:09:55

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

我发现(根据此 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 有必要:

  • 将内容类型设置为“application/json”并将
  • 方法设置为“POST”

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

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

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 :

  • set the content type to 'application/json' and
  • set the method to 'POST'

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.

遥远的她 2024-11-07 04:09:55

我意识到这有点老了,但我也对这个问题很头疼,即使输出类型被指定为 Json,它仍然被包装在 XML 中。对我来说,上述解决方案的问题是我需要通过 GET 来调用它。

不使用网络服务时的解决方案适合我。
我所做的只是创建另一个 aspx 页面并将其添加到我的项目中,在 aspx 页面中我构建了 json 对象并执行了以下操作(全部在 Page_Load 方法内):

Response.ContentType = "application/json";
Response.Write(JsonObject.ToString());
Response.End();

然后从客户端调用“页面”,您只需使用 URL 例如。 “http://aspjsonpage.aspx?parameter=value”

你就得到了你需要的 Json。

I realise this is somewhat old but I too was banging my head at this problem where even though the output type was specified as Json it was still being wrapped in XML. The problem with the solutions above for me was that I needed for it to be called by a GET.

The solution while not using a web service does the job for me.
What I did was just create another aspx page and added it to my project, in the aspx page I built up the json object and did the following (All inside the Page_Load method):

Response.ContentType = "application/json";
Response.Write(JsonObject.ToString());
Response.End();

Then to call the "page" from the client you simply use the URL eg. "http://aspjsonpage.aspx?parameter=value"

And you get the Json you need.

九厘米的零° 2024-11-07 04:09:55

您好,我在使用设置为返回 json 的 Web 服务时遇到了同样的问题,.net 仍然将其包装在 XML 包装器中。我的老师告诉我正确的答案,那就是重新安装.NET Framework 4.0,所有问题都解决了。
感谢何塞·比阿斯!!!!

Hi i just have the same problem when using a webservice set to return json, .net still wraps it in an XML wrapper. And my teacher tells me the correcta answer, that is to reinstalr tha .NET framework 4.0 and all problems was solve.
thans José Beas!!!!

好久不见√ 2024-11-07 04:09:55

非常相似的问题,涉及很多时间。我使用的是 .net2.0,并且 Web 服务无法在运行 Windows Server 2008 R2(运行 IIS 7.5)的生产服务器上运行。 Web 服务返回 XML 的所有问题都必须通过 web.config 来处理。我构建了一个基于 ajax 的 Web 应用程序,并将配置文件复制到我的配置中,它运行良好。需要更新的代码行如下:

<configSections>
    <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
        <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
            <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
            <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
                <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere"/>
                <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
                <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
                <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
            </sectionGroup>
        </sectionGroup>
    </sectionGroup>
</configSections>

在 web.server 中:

<httpHandlers>
        <remove verb="*" path="*.asmx"/>
        <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
    </httpHandlers>
    <httpModules>
        <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    </httpModules>

和:

<system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <modules>
        <remove name="ScriptModule"/>
        <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    </modules>
    <handlers>
        <remove name="WebServiceHandlerFactory-Integrated"/>
        <remove name="ScriptHandlerFactory"/>
        <remove name="ScriptHandlerFactoryAppServices"/>
        <remove name="ScriptResource"/>
        <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    </handlers>
</system.webServer>

我在生产服务器上安装了 Microsoft Visual Web Developer 2008 并创建了一个新网站,然后选择了 ASP.NET Web 服务。我认为如果您在生产服务器上创建一个项目并且它可以工作,那么配置应该一切正确。希望这有帮助。

Very similar issue with many hours involved. I was using .net2.0 and the web services would not work on the production server running Windows Server 2008 R2 running IIS 7.5. All the issues with the web service returning XML had to deal with the web.config. I build an ajax based web application and copied the config file over to my config and it worked perfectly. The lines of code that needed to be updated are below:

<configSections>
    <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
        <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
            <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
            <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
                <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere"/>
                <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
                <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
                <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
            </sectionGroup>
        </sectionGroup>
    </sectionGroup>
</configSections>

in the web.server:

<httpHandlers>
        <remove verb="*" path="*.asmx"/>
        <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
    </httpHandlers>
    <httpModules>
        <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    </httpModules>

and:

<system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <modules>
        <remove name="ScriptModule"/>
        <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    </modules>
    <handlers>
        <remove name="WebServiceHandlerFactory-Integrated"/>
        <remove name="ScriptHandlerFactory"/>
        <remove name="ScriptHandlerFactoryAppServices"/>
        <remove name="ScriptResource"/>
        <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    </handlers>
</system.webServer>

I installed Microsoft Visual Web Developer 2008 on the production server and created a new web site, then selected the ASP.NET Web Service. I figured that if you create a project on the production server and it works, the config should have everything correct. Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文