views:

70

answers:

3

Hi,

I have been looking on how to consume a webmethod using the $.ajax call using this code below:

$.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "WebService.asmx/HelloWorld",
            data: "{}",
            dataType: "json",
            success: function(msg) {
                alert(msg.d);
            }
        });

However when I tried to change the type from "POST" to "GET", the call didn't go through. Can someone please point out a reason why this happens?

+4  A: 

By degault GET requests are disabled for ASP.Net AJAX Web services, ScottGu has an excellent blog entry on this, including how to bypass that security if that's what you're after.

Here's an example fix, by setting UseHttpGet on the ScriptMethodAttribute:

[WebMethod, ScriptMethod(UseHttpGet=true)] 
public string HelloWorld() 
{
  return "Hello World";
}
Nick Craver
Adding the UseHttpGet did get the call through...but then being less secure... I will use POST method instead it doesn't make difference to me which one I will be using. But thanks for the answer
Ryan
A: 

have you tried checking the server side code to see where it pulls values from? it may not respond depending on the method used.

also an aside: if you are retrieving data from a web service, the logically correct method is usually GET.

Scott M.
+1  A: 

As Nick wrote you can use ScriptMethodAttribute or you can enable GET request processing in web.config:

<webServices>
    <protocols>
        <add name="HttpGet"/>
        <add name="HttpPost"/>
    </protocols>
</webServices>
Pavel Morshenyuk