2

I'm new to JQuery and Web Services. My question is (in the following code), why does url: "WebService1.asmx/WebMethod" work through this JQuery code but if I type that URL directly in my browser, it says the path cannot be found? I have to manually click on the "Invoke" button for the service in the browser in order to call the method and then it navigates to WebService1.asmx/WebMethod.

 $(document).ready(function () {
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "WebService1.asmx/WebMethod",
            data: "{}",
            dataType: "json",
            success: function (msg) {
                alert(msg.d);
            },
            error: function (errormessage) {
                alert("got an error");
            }
        });
    });
user1620141
  • 315
  • 1
  • 2
  • 16
  • possible duplicate of [Calling ASP.NET server side method via JQuery](http://stackoverflow.com/questions/886903/calling-asp-net-server-side-method-via-jquery) – jrummell Aug 28 '12 at 17:22

2 Answers2

3

In case of ajax call, you are invoking the url as POST request. Whereas while accessing the url directly in browser, you are invoking the url as GET request. Your service may accept POST request with empty json array as a parameter which is missing while invoking as GET request.

sundar
  • 1,760
  • 12
  • 28
2

POST and GET! The service is listening for different HTTP verbs and responding accordingly.

Pressing the Invoke sends a POST message to the service while typing it in the bar sends a GET message to the service.

Change the query with : type: "GET", and you'll see.

Preet Sangha
  • 64,563
  • 18
  • 145
  • 216