3

I am trying to call a C# method from java script ,I am new to web development and after a bit of searching decided on using jquery to do the same,the way I try to call the method is:

$.ajax({
          type: "POST",
          url: "Default.aspx/IncrementJ",
          data: "{}",
          contentType: "application/json; charset=utf-8",
          dataType: "json",
          success: function(msg) {
            alert("success!")
          }
        });

here IncrementJ is my function name defined in C# which I want to call.here is the definition:

 [WebMethod]
    public static  void IncrementJ()
    {

        try
        {
            j++;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

when I run my program web console throws an error "cannot locate resource incrementJ";please tell me where I am going wrong,

Thanks .

5
  • I am assuming the service is running on your machine? In that case try changing the url in the ajax call to "localhost:8080/Default.aspx/IncrementJ". I specified the port number as 8080 but you should substitute that with the real port number at which your local server is running that is hosting the web service you are trying to consume. Commented Apr 11, 2012 at 12:19
  • Is the script in a external file? If so, take a look at this: stackoverflow.com/questions/5228106/… Commented Apr 11, 2012 at 12:22
  • yes the server is running,and I am able to get data from the server,and yes the script is in asps.cs file Commented Apr 11, 2012 at 12:27
  • aspx*,I am using .net framework Commented Apr 11, 2012 at 12:28
  • yes the url was wrong ,thanks for the link! Commented Apr 11, 2012 at 12:33

2 Answers 2

4

Given your comments that the error status 404 (Not Found), can infer that the error is on behalf of the calling script:

"Http Response Codes for Dummies"

50x: we messed up. 
40x: you messed up. 
30x: ask that dude over there. 
20x: cool.

So, given that the script cannot find the webmethod, I think its fair to deduce that it is looking in the wrong location. Try putting a relative path when referencing Default.aspx/IncrementJ.

Sign up to request clarification or add additional context in comments.

1 Comment

yes the path was wrong,asp.net sometimes messes with the paths,thanks again tho
1

One thing I see is you need to remove the quotes around the data object .. it should be an empty JS object e.g {} not "{}"

1 Comment

I tried that,but still the same same erroe i.eFailed to load resource: the server responded with a status of 404 (Not Found)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.