真正的同步 ajax 调用
Real synchronous ajax call
我有一种情况,我不知道我是否接近正确,但事情是这样的:我想要第二个 POST 发生,但前提是第一个 POST 说好.
function save_match(slot, save) {
listItems.each(function(idx, li) {
var player = $(li);
if(i >= 0) {
// do some work
} else {
// post that should prevent second post from executing, depending on `return_msg`
$.post(..., function(return_msg) {
if(return_msg == "not_ok") {
// I did this in hope that it will do the trick, but no
location.reload();
}
}
);
}
});
// do a little work
$.ajax({
...
});
}
我试图放入一个繁忙的循环,但这会冻结浏览器。我想让第一个 POST 调用同步(但是不会让 //do some work
执行,直到 POST returns,其中 93% returns ok
,但我看不到另一种选择,如果可以,请告诉我),这样如果 return 的 return 就不会发生第二个 POST第一次调用不行。
所以,我发现了这个问题:how to make a jquery “$.post” request synchronous,它说它的最佳答案已被弃用,并提供了一些会阻止 UI 的内容。但是,我想阻止代码执行第二次调用!
如何在 2015 年做到这一点?
一种方法是使 ajax 同步,但不推荐这样做。您可以在 ajax 调用时设置 async: false
。
另一种方法是将一个 ajax 请求放在另一个请求的成功回调中。一个简单的例子是:
$.ajax({
url: "ajaxUrl",
type: "post",
success: function (data) {
if (data == "OK") {
//do other ajax
}
else {
}
},
error: function (jqxhr) { }
});
对于您的情况,上面的示例可能就足够了。要获得更强大和可扩展的解决方案,您可以使用 jQuery.Deferred。针对您的情况的一个简单示例:
var def = $.Deferred(); //Create $.Deferred;
$.ajax({
url: "ajaxUrl",
type: "post",
success: function (data) {
if (data == "OK")
def.resolve(); //Let deferred know it was resolved with success
else
def.reject(); //Let deferred know it ended in a failure
},
error: function (jqxhr) { }
});
def.done(function () {
//this will only run when deferred is resolved
}).fail(function(){
//this will only run when deferred is rejected
}).always(function(){
//this will run when deferred is either resolved or rejected
})
当 return_msg
不等于 not_ok
时,您会提醒 "Error!"。如果消息是 ok
或 not_ok
,那就是你想要第二个 post 的地方,而且你似乎已经知道如何 post。
$.post(..., function(return_msg) {
// check for the success, as null or something else would be an error too
if(return_msg == "ok") {
// This is where you would do another post
$.post(..., function() {});
} else {
// This is where the error should be.
}
}
);
我有一种情况,我不知道我是否接近正确,但事情是这样的:我想要第二个 POST 发生,但前提是第一个 POST 说好.
function save_match(slot, save) {
listItems.each(function(idx, li) {
var player = $(li);
if(i >= 0) {
// do some work
} else {
// post that should prevent second post from executing, depending on `return_msg`
$.post(..., function(return_msg) {
if(return_msg == "not_ok") {
// I did this in hope that it will do the trick, but no
location.reload();
}
}
);
}
});
// do a little work
$.ajax({
...
});
}
我试图放入一个繁忙的循环,但这会冻结浏览器。我想让第一个 POST 调用同步(但是不会让 //do some work
执行,直到 POST returns,其中 93% returns ok
,但我看不到另一种选择,如果可以,请告诉我),这样如果 return 的 return 就不会发生第二个 POST第一次调用不行。
所以,我发现了这个问题:how to make a jquery “$.post” request synchronous,它说它的最佳答案已被弃用,并提供了一些会阻止 UI 的内容。但是,我想阻止代码执行第二次调用!
如何在 2015 年做到这一点?
一种方法是使 ajax 同步,但不推荐这样做。您可以在 ajax 调用时设置 async: false
。
另一种方法是将一个 ajax 请求放在另一个请求的成功回调中。一个简单的例子是:
$.ajax({
url: "ajaxUrl",
type: "post",
success: function (data) {
if (data == "OK") {
//do other ajax
}
else {
}
},
error: function (jqxhr) { }
});
对于您的情况,上面的示例可能就足够了。要获得更强大和可扩展的解决方案,您可以使用 jQuery.Deferred。针对您的情况的一个简单示例:
var def = $.Deferred(); //Create $.Deferred;
$.ajax({
url: "ajaxUrl",
type: "post",
success: function (data) {
if (data == "OK")
def.resolve(); //Let deferred know it was resolved with success
else
def.reject(); //Let deferred know it ended in a failure
},
error: function (jqxhr) { }
});
def.done(function () {
//this will only run when deferred is resolved
}).fail(function(){
//this will only run when deferred is rejected
}).always(function(){
//this will run when deferred is either resolved or rejected
})
当 return_msg
不等于 not_ok
时,您会提醒 "Error!"。如果消息是 ok
或 not_ok
,那就是你想要第二个 post 的地方,而且你似乎已经知道如何 post。
$.post(..., function(return_msg) {
// check for the success, as null or something else would be an error too
if(return_msg == "ok") {
// This is where you would do another post
$.post(..., function() {});
} else {
// This is where the error should be.
}
}
);