jQuery Ajax到asp.net asmx web服务抛出请求格式无效:application / json

前端之家收集整理的这篇文章主要介绍了jQuery Ajax到asp.net asmx web服务抛出请求格式无效:application / json前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个jQuery调用带有整数的asp.net webservice.
在我们的移植应用程序上移植到.net 4.0我无法接到此调用.
我可以调用一个没有参数但是向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()

我在一个空白项目中创建了完全相同的代码,并且工作正常.我无法在web.config中看到任何空白项目添加内容会产生影响.

Jquery代码

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

我的网络服务代码

<ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolBoxItem(False)> _
Public Class WebService1
    Inherits WebService

    <WebMethod()>
    Public Function Test(ByVal code As Integer) As String
        Return "success"
    End Function

    <WebMethod()>
    Public Function Hello() As String
        Return "hello"
    End Function    
End Class

Web配置

<?xml version="1.0" encoding="UTF-8"?>
<configuration>  
    <appSettings>
    </appSettings>
    <connectionStrings>
    </connectionStrings>
    <system.web>
        <httpRuntime enableVersionHeader="false" />
        <httpCookies httpOnlyCookies="true" requireSSL="false" lockItem="true" />
        <trace enabled="false" pageOutput="true" requestLimit="40" localOnly="true"/>

        <httpModules>
        </httpModules>
        <compilation debug="true" strict="true" explicit="true" targetFramework="4.0">

        </compilation>
        <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID">
        </pages>

        <authentication mode="Forms">

        <httpHandlers>     
        </httpHandlers>
    </system.web>

    <system.webServer>

    <validation validateIntegratedModeConfiguration="false" />
    <modules>
    </modules>
    <handlers>
    </handlers>
    <httpErrors errorMode="Custom" > 
    </httpErrors>
    </system.webServer>
</configuration>

解决方法

DOH,我在错误的web.config中工作.

像SO上的很多问题一样,解决方案是添加以下内容.

<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,PublicKeyToken=31BF3856AD364E35" />
        </handlers>
  </system.webServer>

猜你在找的jQuery相关文章