0

Using query ajax to call a webmethod in an ASP.NET page works well if the HTTP request is a POST. If a HTTP GET request is used the Page_Load event runs instead of the webmethod. Any ideas?

Here is the Ajax

$.ajax({
        type: "GET",
        url: "http://local.proleaguesports.pagefad.com/AjaxTesting.aspx/receivermethod",
        data: "{'test':'MyName'}",
        contentType: "application/json",
        dataType: "json",
        success: mycallback,
        error: handler
    });

AND here is the code behind in C#

public partial class AjaxTesting : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("Page_Load runs instead of the receivermethod below");
    }

    [WebMethod]
    [ScriptMethod(UseHttpGet = true)]
    public static string receivermethod()
    {

        return "test received";

    }
}
John Fisher
  • 22,355
  • 2
  • 39
  • 64
  • Why are you using Get? If you do some reading on the internet, you'll find that it opens up some significant security vulnerabilities. Won't "Post" work? – John Fisher Aug 28 '09 at 17:27
  • John, could you recommend a good Security and HTTPGET article? I think I will use POST. but, now Im curious :) –  Aug 28 '09 at 18:03
  • your ajax call is passing back data, but your WebMethod has no parameters – Martin Aug 28 '09 at 18:20
  • Check this out: http://stackoverflow.com/a/752815/626533 – Cosmin Apr 25 '12 at 17:00

1 Answers1

0

You might consider moving your webmethods into a separate web service/asmx. I heard somewhere that calling a webmethod in an aspx causes the entire page to be reloaded, though I can't find a confirming reference for that at the moment.

3Dave
  • 28,657
  • 18
  • 88
  • 151
  • No it doesn't. I have a few web methods that work just fine and don't reload the page. – krishna Aug 28 '09 at 18:21
  • I've just set a breakpoint in the page_load method of a page that has several webmethods. Calling the webmethod does, indeed, trigger Page_Load(), and I assume everything else that implies. – 3Dave Sep 17 '09 at 14:34