.net Json返回xml

.net Json返回xml,.net,web-services,json,.net,Web Services,Json,我知道我不是第一个遇到这个问题的人,但我似乎找不到有效的解决办法 当使用webservice集返回json时,.net仍然将其包装在XML包装器中 我搜索并尝试了很多东西 我试着向我的web.config添加各种httphandler设置,正如某些帖子中所建议的那样,但这些都没有效果。我也不认为这是必要的,因为我正在开发一个全新的win7/iis7.5/.net4设备。从.NET3.5开始,我就知道应该不会有任何问题。但是有 我尝试了使用和不使用responseformat.json装饰。我的W

我知道我不是第一个遇到这个问题的人,但我似乎找不到有效的解决办法

当使用webservice集返回json时,.net仍然将其包装在XML包装器中

我搜索并尝试了很多东西

  • 我试着向我的web.config添加各种httphandler设置,正如某些帖子中所建议的那样,但这些都没有效果。我也不认为这是必要的,因为我正在开发一个全新的win7/iis7.5/.net4设备。从.NET3.5开始,我就知道应该不会有任何问题。但是有
  • 我尝试了使用和不使用responseformat.json装饰。我的Web服务返回有效的json(在提取字符串后,我可以使用parsejson对其进行解析)
  • 我尝试显式设置contenttype和datatype。这会导致一个错误,抱怨响应是无效的json。没错 发生的事情非常令人困惑,在IE中,至少在我的devbox上,响应返回为xml文档,我可以使用msg.text轻松获取json字符串,但在生产中,我在FF中进行了测试,它返回为文档,没有“text”属性

    这是我的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); 
        }
    });
    
    $.ajax({
    错误:函数(xhr、ajaxOptions、thrownError){
    警报(xhr.status+'-'+xhr.statusText);
    警报(thrownError);
    },
    url:“+”/JackJill“,
    contentType:“应用程序/json”,
    成功:功能(msg){
    警报(msg.d);
    }
    });
    
    那么:我如何简单地要求.net返回一个有效的常规json字符串,而不是包装它呢。我相信这会解决所有问题。它还将使我的服务更容易被全世界访问,因此他们不必做任何特殊的解析

    非常感谢您的建议和指点

    诚恳

    编辑: 下面是我刚刚测试的一个示例Web服务:

    <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> <WebMethod()> Public Function JackJill() As String
        Return "[{""Name"":""Jack""},{""Name"":""Jill""}]"
    End Function
    
    公共函数JackJill()作为字符串
    返回“[{”“Name”“:”“Jack”“},{”“Name”“:”“Jill”“}”
    端函数
    
    然后当我把它放到浏览器里

    我明白了

    
    [{“姓名”:“杰克”},{“姓名”:“吉尔”}]
    

    为什么这里有这么多xml内容?

    我意识到这个问题很老了,但以防将来有人看到这个答案

    我发现(根据这一点和这一点),如果您希望使用.ASMX中的JSON,有必要:

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

    伊斯曼说,他不想把自己的服务局限于发布请求,但(根据这两份参考资料)如果您希望使用.ASMX并接收JSON,那么您就必须这样做。

    我意识到这有点过时,但我也对这个问题感到头疼,尽管输出类型被指定为JSON,但它仍然被包装在XML中。上述解决方案对我来说的问题是,我需要一个GET来调用它

    这个解决方案在不使用web服务的情况下完成了这项工作。 我所做的只是创建另一个aspx页面并将其添加到我的项目中,在aspx页面中,我构建了json对象并执行了以下操作(都在page_Load方法中):

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


    您可以得到所需的Json。

    嗨,我只是在使用webservice集返回Json时遇到了同样的问题,.net仍然将其包装在XML包装器中。我的老师告诉我正确的答案,那就是重新安装tha.NETFramework4.0,所有的问题都解决了。
    桑斯·何塞·比亚斯

    非常类似的问题,涉及很多小时。我使用的是.net2.0,web服务在运行IIS 7.5的Windows server 2008 R2的生产服务器上无法运行。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>
    
    
    
    以及:

    
    

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

    我猜您的服务WebService.asmx正在返回XML。你能提供详细情况吗?代码和配置会很有帮助。谢谢paulczy,我刚刚在我的原始帖子中添加了一些示例代码。谢谢你的关注!看看这篇博文:我以前已经读过这篇博文了。这就是为什么我尝试将contenttype“application/json”添加到ajax调用中。但这会导致500个内部服务器错误。如果不设置contenttype,那么
    msg
    将成为IE中的xml文档。顺便说一句,如果我想通过“GET”轻松访问此Web服务,它不应该依赖contenttype,因为我不知道任何需要设置contenttype以获得正确响应的GET请求。我现在还在msconnect a shame中添加了一个建议,但是非常正确。正如我在对Paulczy的回答的评论中已经提到的那样。没错,我还使用了
    response.end
    hack。但我后来搬到了WCF,至今还没有回头;-)
    <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>
    
    <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>