错误手札 - Request format is invalid: application/json; charset=UTF-8


jQuery Ajax to asp.net asmx web service throws Request format is invalid: application/json

解决方案来自:stackoverflow (http://stackoverflow.com/questions/11992139/jquery-ajax-to-asp-net-asmx-web-service-throws-request-format-is-invalid-applic

在IIS服务器上.NET4.0环境下,用Ajax请求asmx的web服务,出现以下错误:

System.InvalidOperationException: Request format is invalid: application/json; charset=UTF-8. 
at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters() 
at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

Ajax代码:

$.ajax({
    type: "POST",
    url: "/WebService1.asmx/Test",
    data: JSON.stringify({"code": 1234}),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (msg) {
        alert(msg);
    }
});

在web.config中修改配置,解决方案:

<system.webServer>
        <handlers>
            <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" />
        </handlers>
  </system.webServer>


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM