jQuery Ajax to asp.net asmx web service throws Request format is invalid: application/json

Viewed 14305

I have jquery call an asp.net webservice with an integer. On our legaacy application which was ported to .net 4.0 I cannot get this call to work. I can call a method which has no parameters but sending data to the web method returns the following error:

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()

I created exactly the same code in a blank project and it worked fine. I couldnt see anything in the web.config that the blank project added that would make a difference.

the Jquery Code

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

My Web Service Code

<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 Config

<?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>
2 Answers
Related