I set up a page method and am trying to call it through jQuery. Sometimes I get to the success function and sometimes to the error function, seems random to me. In any case, the response contains the entire page mark-up.
I tried using both $.ajax
and ScriptManager
with same results.
I also tried the idea here: Call ASP.NET PageMethod/WebMethod with jQuery - returns whole page and nothing.
Here is the JavaScript code:
$(document).ready(function() {
$(".tree").dynatree({
onActivate: function(node) {
$('#title').val(node.data.title);
$.ajax({
type: "POST",
url: window.location.href + "/GetData",
data: "{'ID':22}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response) { alert(response); },
error: function() { alert('Error!'); }
});
}
});
});
And here is the c# code:
[WebMethod]
public static string GetData(string ID)
{
if (string.IsNullOrEmpty(ID))
throw new Exception("No ID passed!");
return "Test";
}
Edit: Well I got it working. I changed the parameter type from int to string and now the method is called. I can just do int.Parse later, but why does this even happen?