Skip to content Skip to sidebar Skip to footer

C# Webmethod Unable To Be Called With JQuery AJAX

I am trying to call a webmethod I created. The problem I'm having is that the ajax call never calls my webmethod; this is strange to me because I have another webmethod located in

Solution 1:

Make sure you have a reference to jQuery if you want to make a jQuery ajax call.

Also, the parameters of your WebMethod need to match the parameter name you are passing in. The parameter is appName:

 $.ajax({
    type: "POST",
    url: "CS.aspx/getAppId",
    data: '{appName: "' + $("#<%=txtUserName.ClientID%>")[0].value + '" }',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: OnSuccess,
    error: function (response) {
        alert(response.d);
    }
});

And in the WebMethod it is appName as well:

[System.Web.Services.WebMethod]
public static string getAppId(string appName)
{
    return "this is a string";
}

In your code, there is a mismatch.

I would also make a habit of closing your div and form tags.


Solution 2:

You need to place this service in a WCF or ASMX service and create a JavaScript proxy for it. See http://msdn.microsoft.com/en-us/library/bb532367(v=vs.90).aspx


Post a Comment for "C# Webmethod Unable To Be Called With JQuery AJAX"