开发者

Page method not called

开发者 https://www.devze.com 2023-03-07 03:55 出处:网络
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

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:

[WebMet开发者_StackOverflowhod]
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?


What's happening is that setting data to {} in the jQuery call is the JSON equivalent of setting it to NULL. In that case, there is no webmethod that accepts null and the call fails.


I'm doing something very similar in my current app and the only difference I can see is that i have the following extra option in my ajax call :

beforeSend: function(xhr) { xhr.setRequestHeader("Content-type", "application/json; charset=utf-8"); }

Hope that helps...

0

精彩评论

暂无评论...
验证码 换一张
取 消