Search code examples
asp.netajaxwebmethod

Simple AJAX not working


I have this AJAX code, but it doesn't seem to throw the 'alert' method. Instead, nothing happens. I looked at it with Fiddler and got this error message: {"Message":"There was an error processing the request.","StackTrace":"","ExceptionType":""}

I'm trying to call a web method in the code-behind called MyWebMethod:

 $.ajax({   type: "POST",
            url: "Test.aspx/MyWebMethod",
            data: "{" + username + "}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",

            success: function() {
                alert("success");
            },

            fail: function() {
                alert("Fail");
            }
 });

The web method worked fine when I had a script manager on the page, but I want to remove the script manager and thought that using AJAX would be the best way.

Thanks


Solution

  • I think if you change fail to error, you'll get the second alert box.

    [Edit] I think if you then change

    data: "{" + username + "}"
    

    to

    data: "{ 'username': '" + username + "' }"
    

    you'll get the first alert, although it's hard to know that without seeing the service you're calling.