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

标签 asp.net web-services jquery

我让 jquery 使用整数调用 asp.net web 服务。 在我们移植到 .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

网络配置

<?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, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        </handlers>
  </system.webServer>

关于asp.net - jQuery Ajax 到 asp.net asmx Web 服务抛出请求格式无效 : application/json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11992139/

相关文章:

asp.net 成员(member)提供程序通过 UserId 获取用户名

asp.net - Lucene.NET --> 拒绝访问段

android - 向 webservice 发送 json,接收 null。 - 安卓

javascript - 将表单的复选框值作为 JSON 传递?

c# - ASP .Net MVC中的错误处理

asp.net - "Add as Link"JavaScript 文件在调试中返回 404

java - 网比 : How to create a web service client that consumes a SSL protected Metro service?

java - 从 Android 的 Jersey Resftul Web 服务传递和接收 JSON 对象

jquery - 图像轮播中的元素对齐

javascript - 代码执行的同步延迟