Jquery Ajax Call does not Call the function in .CS file

Always i get the alert in the “error”. When i debugged i get the type,url as undefined. can anyone help me why that method is not getting called??

$(document).ready(function () {
    $("#btnajaxcall").click(function () {
            type: "POST",
            url: "Default.aspx/jQueryAjaxCalledMethod",
            contentType: "application/json; charset=utf-8",
            data: "{}",
            dataType: 'json',
            success: function () { alert('success') },
            error: function () { debugger; alert('failure'); return false; }

public void jQueryAjaxCalledMethod()
   //SOME CODE HERE       

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

If im correct you should be using static method for these purposes, so function in your code behind should look like this

public static void jQueryAjaxCalledMethod()
   //SOME CODE HERE       

If you still get some errors take a look on this guy blog Encosia maybe you’ll find there a solution

Solution 2

The jquery Ajax method is going to post your data in json format using the plain html protocol. ASP.NET will be expecting to unwrap a SOAP request to pass to the webmethod. Thus the error. You should use an MVC action instead, as suggested in one of the comments.
EDIT:On further investigation ASP.Net has an attribute that will allow the web method to be called:
Use this attribute on the class and it might solve your problem.

Solution 3

Hi all i just used the jquery file hosted with google.

It worked out fine.

Previously i was using the jquery version 1.7.1 that i had downloaded and stored in my local. I also saw a lot of questions in the forum that this particular ajax call is quite not happening properly with .NET 4. I am not sure and forgive me if i am wrong but i do have a feeling that 1.7.1 in this case is not properly working with ASP.NET 4.

P.S -> I used this in the script tag –>

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply