微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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>

原文地址:https://www.jb51.cc/jquery/177593.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐