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

sky*_*oot 7 asp.net jquery web-services

我有一个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()
Run Code Online (Sandbox Code Playgroud)

我在一个空白项目中创建了完全相同的代码,并且工作正常.我无法在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);
    }
});
Run Code Online (Sandbox Code Playgroud)

我的网络服务代码

<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
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

sky*_*oot 10

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, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        </handlers>
  </system.webServer>
Run Code Online (Sandbox Code Playgroud)


Rem*_*tec 9

我有同样的问题,并最终运行此命令...

C:\Windows\Microsoft.NET\Framework64\v4.0.30319>aspnet_regiis.exe -i
Run Code Online (Sandbox Code Playgroud)

请注意,您需要在管理员模式下使用命令提示符才能使用它.