Using both POST and GET Ajax calls the same WebMethod in the ASMX web service

I cannot name the web service method from Ajax using POST and GET.

At first, only POST will work, and GET will result in this error:

{"Message": "An attempt was made to call the \ u0027getData \ u0027 method using a GET request that is not allowed.", "StackTrace": "at System.Web.Script.Services.RestHandler.GetRawParams (WebServiceMethodData methodData, HttpContext context) \ r \ n
in System.Web.Script.Services.RestHandler.ExecuteWebServiceCall (HttpContext context, WebServiceMethodData methodData) "," ExceptionType ":" System.InvalidOperationException "}

I fixed this by adding this attribute: [ScriptMethod(UseHttpGet=true)]but now GET is causing this error:

{"Message": "An attempt was made to call the \ u0027getData \ u0027 method using a POST request that is not allowed.", "StackTrace": "at System.Web.Script.Services.RestHandler.GetRawParams (WebServiceMethodData methodData, HttpContext context) \ r \ n
in System.Web.Script.Services.RestHandler.ExecuteWebServiceCall (HttpContext context, WebServiceMethodData methodData) "," ExceptionType ":" System.InvalidOperationException "}

So is it true that you can only use POST or GET, not both of Ajax? Does anyone know why this is happening or is there a workaround?

Thanks in advance!

+3
4

ASMX GET, POST, , GET JSON. JSON AJAX.

JSON GET, HttpHandler WCF.

, , , , JSON GET.

+2

- ASMX JSON GET, .

<System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class TestService
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    <ScriptMethod(ResponseFormat:=ResponseFormat.Json, UseHttpGet:=True)> _
    Public Function HelloWorld() As String
        Return "Hello World"
    End Function


End Class
+1

-, GET POST

<WebMethod()> _
<ScriptMethod(UseHttpGet:=True)> 
Public Function HelloWorld_GET() As String
    Return "Hello World"
End Function

<WebMethod()> _
Public Function HelloWorld_POST() As String
    Return "Hello World"
End Function
+1

You should try this with WCF. ASMX web services are now considered "obsolete technologies," and Microsoft said they are now in "maintenance mode" and errors are unlikely to be fixed.

0
source

Source: https://habr.com/ru/post/1729180/


All Articles