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.
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: