开发者

JQuery Ajax POST no longer hits ServerSide asp.net Method after adding parameters

Before adding any parameters I had this POST method hitting my serverside code whenever a user would set focus from one control to another. This was good but i need to pass data of the id and value. These values are being populated from what gather in my failure growl.

The issue is that I don't exactly know why it will no longer hit my serverside routine. I did look around stackoverflow a good deal for some help, i did clean up a few things but I couldnt quite pinpoint exactly where my issue is.

$(document).ready(function () {
    var id;
    var value;

    $(".tpControl").blur(function () {

        id = this.id;
        value = $(this).val();

        var postdata = { identifier: id, controltext: value };

        $.ajax({
            type: "POST",
            url: "TimePoints.aspx/ClientSideSave",
            contentType: "application/json; charset=utf-8",
            data: postdata,
            dataType: "json",
            success: function (msg) {
                if (msg.hasOwnProperty("d")) {
                    OnSuccess(msg.d);
                } else {
                    OnSuccess(msg);
                }
            },
            error: OnFailure
        });
    });

    function OnSuccess(result) {
        $.growlUI('AutoSave Successful');
    }

    function OnFailure(result) {
        $.growlUI("ID: " + id, "Answer: " + value);
    }
});

My Server side is pretty simple

    [WebMethod]
    public static void ClientSideSave(string identifier, string controltext)
    {
       //Bunch of code, shouldn't affect anything.
    }

Thank you in advance.开发者_JAVA百科 Any help would be great, im pretty new to the world of Jquery and Ajax.


You were close, but remember you are posting JSON, not form value collections. This means JSON strings are quoted during transport and in your example, will be quoted on the backend, probably not what you really want:

var postdata = "{ 'identifier': 'id', 'controltext': 'value' }";

3 mistakes to avoid when using jQuery with ASP.NET AJAX

If you want use JSON(JavaScript Object Notation) the right way, then you should follow this example:

Using complex types to make calling services less… complex

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜