jquery 的 $.get 是异步的吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13671798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is $.get of jquery asynchronous?
提问by ksm001
I was wondering whether I should use asynchronous calls on my website or not. I know to specify this explicitly I need to use
我想知道我是否应该在我的网站上使用异步调用。我知道要明确指定这个我需要使用
$.ajax
however initially I tried using $.get
and although the server had to return lots of information, my browser didn't stuck and I could navigate without any problems.
但是最初我尝试使用$.get
,虽然服务器必须返回大量信息,但我的浏览器没有卡住,我可以毫无问题地导航。
I searched a little bit online about it, however I'm still not 100% certain about the difference between the two.
我在网上搜索了一下,但我仍然不能 100% 确定两者之间的区别。
If $.get
is asynchronous then what's the point of $.ajax
? And if it's not, then again seeing how I had no navigation problems with $.get
, what's the point of using $.ajax
?
如果$.get
是异步的,那么有什么意义$.ajax
呢?如果不是,那么再次看到我没有导航问题$.get
,使用 有$.ajax
什么意义?
thanks in advance
提前致谢
回答by T.J. Crowder
Yes, $.get
is asynchronous. From the documentation:
是的,$.get
是异步的。从文档:
This is a shorthand Ajax function, which is equivalent to:
$.ajax({ url: url, data: data, success: success, dataType: dataType });
这是一个简写的 Ajax 函数,相当于:
$.ajax({ url: url, data: data, success: success, dataType: dataType });
...and as that doesn't have the async
option, async
defaults to true
. (Note that async
will be going away entirely in a future version of jQuery; it will alwaysbe true.)
...由于没有async
选项,async
默认为true
. (请注意,这async
将在未来版本的 jQuery 中完全消失;它永远是真实的。)
If
$.get
is asynchronous then what's the point of$.ajax
?
如果
$.get
是异步的,那么有什么意义$.ajax
呢?
$.ajax
gives you control over lot more options. $.get
is just a shortcut.
$.ajax
让您可以控制更多选项。$.get
只是一个捷径。
回答by Jo?o Silva
回答by Connor Doyle
As stated by the jQuery
documentation, $.get
is a convenience wrapper for the more low-level $.ajax
.
如jQuery
文档所述,$.get
是更底层$.ajax
.
回答by Cosmo Arun
$.ajax with GET method, How about this with a promise object?
$.ajax 与 GET 方法,这个与承诺对象如何?
function showResults(name) {
var deferred = $.Deferred, requests = [];
requests.push($.ajax({url:"/path/to/uri/?name=" + name, type: "GET", async: false}).done(function(d) {
//alert or process the results as you wish
}));
$.when.apply(undefined, requests).then(function(d) { console.log(d); /*var d is a promised late return */ deferred.resolve(); });
return deferred.promise();
}
he returned promise object can also be used with $.when(showResults('benjamin')).done(function() { });
for post modifications (like chart/graph settings etc). totally reusable. You may also put this function in a loop of $.deferred requests like,
他返回的承诺对象也可用于$.when(showResults('benjamin')).done(function() { });
后期修改(如图表/图形设置等)。完全可重复使用。您也可以将此函数放入 $.deferred 请求的循环中,例如,
function updateResults() {
var deferred = $.Deferred, requests = [];
requests.push($.ajax(url:"/path/to/names/?nameArr=" + jsonArrOfNames, type: "GET", async: false}).done(function(res) { requests.push(showResults(res[0]));}) );
$.when.apply($, requests).then(function(d) { console.log(d); /*var d is a promised late return */ deferred.resolve(); });
return deferred.promise();
}