如何阻止对AJAX调用(我想它阻止) [英] how to block on ajax call (I want it to block)
问题描述
Ajax使用回调,因为它是 A
同步。
Ajax uses callbacks, as it's A
synchronous.
我要我的电话远程URL阻塞,直到有一些答案,正是因为在阿贾克斯,但没有同步的一部分,或者我应该说我想要做一个JAX电话。
I want my call to the remote URL block until there's some answer, exactly as in Ajax, but without the asynchronous part, or shall I say I want to make a JAX call.
有没有什么技术,使下发生的(用了jQuery)(...用jQuery或其他任何解决方案):
Is there any technique to make the following happen (uses JQuery) (... solution with JQuery or anything else):
function get_data() {
$.ajax({
type : "POST",
url : "/foo"
}).done(function(data, textStatus, jqXHR) {
return data;
}).fail(function(jqXHR, textStatus) {
return null;
});
}
var data = get_data();
// process `data`
我只是想知道 - 想学习
I'm just wondering - want to learn.
其实有次在那里阻塞,直到答复将适合的罚款。我不是说我想要的浏览器来阻止,只是脚本运行。
Actually there are times where blocking until a reply would fit fine. I'm not saying I want the browser to block, just the script runtime.
推荐答案
您可以简单地设置异步:假
布尔使用jQuery(的check的文档)。请注意:在jQuery 1.8,采用异步的:假以jqXHR($ .Deferred)是pcated德$ P $;您必须使用完整/成功/错误回调。
You can simply set the async : false
boolean when using jQuery (check the docs). Take note: As of jQuery 1.8, the use of async: false with jqXHR ($.Deferred) is deprecated; you must use the complete/success/error callbacks.
如果你不想使用jQuery或想了解引擎盖下是怎么回事,阅读这。
If you don't want to use jQuery or want to know what's going on under the hood, read this.
xmlhttp.open("GET","ajax_info.txt",false);
xmlhttp.send();
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
难道不知道为什么你不希望它是异步虽然...
Do wonder why you don't want it to be async though...
这篇关于如何阻止对AJAX调用(我想它阻止)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!