阿贾克斯同步请求 [英] Ajax synchronous requests
问题描述
我已经经历了很多帖子了。有人说,jQuery的1.8后,其去precated。对于一些人来说,它仍然有效。我怎样才能实现这个
I have gone through many posts. Some say that after jquery 1.8, its deprecated. For some, it still works. How can I implement this
$.ajaxSetup({async:false});
duration_reco = $.ajax({
url : newurl,
async : false,
dataType : 'json',
});
console.log(duration_reco.responseText);
它说durtaion_reco是不确定的。我已经checkeed看到duration_reco cotaines有效数据。 Ajax请求的执行同步进行。
It says durtaion_reco is undefined. I have checkeed to see that duration_reco cotaines valid data. the ajax request is executing synchronously.
推荐答案
$。阿贾克斯
支持的许多的不同类型的请求(其中jQuery的电话的运的),并从该用户(正常XHR,XHR与CORS,JSONP)隐藏它们。这是好事,因为它意味着你可以开火 $的任何URL。阿贾克斯()
,而不必担心发生了什么幕后。
$.ajax
supports many different types of requests (which jQuery calls transports), and hides them from the user (normal XHR, XHR with CORS, JSONP). This is good as it means you can fire any URL at $.ajax()
, and not have to worry about what is happening behind the scenes.
不过,在坏并不是所有类型的请求支持所有的选项 $。阿贾克斯()
支持。事情开始失败,看似随意,当这一切发生。这就是发生在这里。
However, it is bad in that not all types of requests support all of the options $.ajax()
supports. Things start to fail, seemingly randomly, when this happens. This is what is happening here.
既然你请求的资源来自另一个域, $。阿贾克斯()
正在一个JSONP请求。要了解JSONP是如何工作的,请参阅<一href="http://stackoverflow.com/questions/3839966/can-anyone-explain-what-jsonp-is-in-layman-terms/3840118#3840118">Can谁能解释什么JSONP是,一般用语?。
Since you're requesting an resource from another domain, $.ajax()
is making a JSONP request. To understand how JSONP works, see Can anyone explain what JSONP is, in layman terms?.
基本上,JSONP请求不同步发生。你可以的只有的执行它们异步运行,这涉及到改变你的code到;
Basically, JSONP requests cannot happen synchronously. You can only perform them asynchronously, which involves altering your code to;
duration_reco = $.ajax({
url : newurl,
dataType : 'json'
}).done(function (obj) {
console.log(obj);
});
同步AJAX请求的坏反正。他们锁住浏览器。有几乎没有理由来使用它们。 避免他们不惜一切代价。 我不能强调这就够了。的
Synchronous AJAX requests are bad anyway. They lock up the browser. There is hardly ever a reason to use them. Avoid them at all costs. I cannot emphasise this enough.
您还应该确保你的的newURL
变量开始任的http://
或 https://开头
;否则将被视为对当前域的路径,而的http://www.yourdomain.com/gdata.youtube.com/feeds/api ...
将被请求。
You should also make sure your newurl
variable starts with either http://
or https://
; otherwise it'll be treated as a path on your current domain, and http://www.yourdomain.com/gdata.youtube.com/feeds/api...
will be requested.
这篇关于阿贾克斯同步请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!