Ho jquery chiamare un webservice asp.net con un numero intero. Sulla nostra applicazione legacy portata a .net 4.0 Non riesco a far funzionare questa chiamata. posso chiamare un metodo che non ha parametri, ma l'invio dei dati al metodo Web restituisce il seguente errore:jQuery Ajax verso asp.net servizio web asmx Il formato richiesta non è valido: application/jones
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()
ho creato esattamente lo stesso codice in un progetto vuoto e ha funzionato bene. Non riuscivo a vedere nulla nel web.config aggiunto dal progetto vuoto che avrebbe fatto la differenza.
il codice 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);
}
});
My Code Web Service
<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>
Potresti pubblicare il tuo file web.config? So dove si trova il problema, ho solo bisogno di una conferma prima di pubblicare la risposta. –
@DarinDimitrov Ho aggiunto il web.config ma ho rimosso i dati sensibili – skyfoot