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>