H_MZ 2014-12-29 14:46 采纳率: 0%
浏览 38

jqtouch中的ajax调用

I have a toggle switch implemented in jQTouch. I want to set the switch position dependinging upon the output of ajax call. I'm calling jquery like below. But it is not working.

var request = $.ajax({
         url: "/cgi-bin/devStat.sh",
         type: "POST",
});
request.done(function(msg){
     alert(msg);
     if(msg[0] === "1")
         $('#myCheckbox1').prop("checked", true);
});
request.fail(function(jqXHR, textStatus) { 
     alert("failed");
});

But I'm not getting anything back. Please help me how to get the information from ajax call.

  • 写回答

1条回答 默认 最新

  • weixin_33744854 2014-12-29 14:54
    关注

    I think your problem is that you are firing the request and got a callback before you add the event handler to it. Try to use a chain of constructs, like this:

    $.ajax({
        url: "/cgi-bin/devStat.sh",
        type: "POST",
    }).done(function (msg) {
        alert(msg);
        if(msg[0] === "1")
            $('#myCheckbox1').prop("checked", true);
    }).fail(function(jqXHR, textStatus) { 
        alert("failed");
    });
    

    See more here:

    Should I use .done() and .fail() for new jQuery AJAX code instead of success and error

    and here:

    deferred.fail()

    评论

报告相同问题?