Search code examples
.netjquerypagemethods

AJAX WebMethod help


I have been trying to access a .NET WebMethod from jQuery AJAX but I cannot get it to work. I have read everything I can find on SO and other sites and nothing seems to be working.

My C# code is such

[WebMethod]
public static string TestAjax()
{
    return "Hello World";
}

and the JavaScript code is

$.ajax({
    type: "POST",
    url: "ManageEvent.aspx/TestAjax",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg)
    {
        alert(msg.d);
    },
    error: function(result)
    {
        alert("error: " + result.status);
    }
});

The end goal was to do a form submission to the database using AJAX without reloading the entire page but at the moment I can't even get it to return back a String. When I click the input button to which this code is attached the error code is called giving a status of 12030. But when I test the code outside of Visual Studio I still error but with a status of 200. Is there anything blatantly obvious in my code that would cause this error?

Thanks a ton

EDIT:

I have gotten the WebMethod to work using ASP.NET Ajax's ScriptManager by installing ASP.NET AJAX 1.0 and calling in my Javascript

PageMethods.TextAjax(OnSuccess, OnFail);

where OnSuccess and OnFail are functions. However I still cannot get jQuery's AJAX to work. It still throws the 12030 error status.


Solution

  • The Web service was missing a [ScriptService] tag which fixed it after installing ASP.NET AJAX