Reputation: 1920
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 .
Upvotes: 3
Views: 565
Reputation: 30488
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
.
Upvotes: 4
Reputation: 22857
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 "{}"
Upvotes: 1