返回 AJAX 调用数据的 JavaScript 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5150571/
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
JavaScript function that returns AJAX call data
提问by Timothy Ruhle
I would like to create a JavaScript function which returns the value of a jQuery AJAX call. I would like something like this.
我想创建一个返回 jQuery AJAX 调用值的 JavaScript 函数。我想要这样的东西。
function checkUserIdExists(userid){
return $.ajax({
url: 'theurl',
type: 'GET',
cache: false,
data: {
userid: userid
},
success: function(data){
return data;
}
});
}
I know I can do this by setting async to false but I would rather not.
我知道我可以通过将 async 设置为 false 来做到这一点,但我宁愿不这样做。
采纳答案by Matt Ball
With jQuery 1.5, you can use the brand-new $.Deferredfeature, which is meant for exactly this.
在 jQuery 1.5 中,您可以使用全新的$.Deferred功能,这正是为此而设计的。
// Assign handlers immediately after making the request, // and remember the jqxhr object for this request var jqxhr = $.ajax({ url: "example.php" }) .success(function() { alert("success"); }) .error(function() { alert("error"); }) .complete(function() { alert("complete"); }); // perform other work here ... // Set another completion function for the request above jqxhr.complete(function(){ alert("second complete"); });
// Assign handlers immediately after making the request, // and remember the jqxhr object for this request var jqxhr = $.ajax({ url: "example.php" }) .success(function() { alert("success"); }) .error(function() { alert("error"); }) .complete(function() { alert("complete"); }); // perform other work here ... // Set another completion function for the request above jqxhr.complete(function(){ alert("second complete"); });
回答by rsp
You can't return data returned by an AJAX call unless you want to call it synchronously (and you don't – trust me). But what you can return is a promiseof a data returned by an AJAX call and you can do it actually in a very elegant way.
您不能返回 AJAX 调用返回的数据,除非您想同步调用它(并且您不想 - 相信我)。但是你可以返回的是一个AJAX 调用返回的数据的承诺,你可以用一种非常优雅的方式来实现它。
(UPDATE:Please note that currently jQuery Promises are not compatible with the Promises/A+ specification- more info in this answer.)
(更新:请注意,当前 jQuery Promises 与Promises/A+ 规范不兼容-此答案中有更多信息。)
Basically you can return the return value of your $.ajax(...) call:
基本上你可以返回你的 $.ajax(...) 调用的返回值:
function checkUserIdExists(userid){
return $.ajax({
url: 'theurl',
type: 'GET',
cache: false,
data: {
userid: userid
}
});
}
and someone who calls your function can use it like this:
调用您的函数的人可以这样使用它:
checkUserIdExists(userid).success(function (data) {
// do something with data
});
See this post of minefor a better explanation and demos if you are interested.
如果您有兴趣,请参阅我的这篇文章以获得更好的解释和演示。
回答by Omer Bokhari
you can pass in a callback function:
你可以传入一个回调函数:
function checkUserIdExists(userid, callback) {
$.ajax({
...
success: callback
});
}
checkUserIdExists(4, function(data) {
});
回答by Michael Sharman
As of jQuery 1.8, the "success", "error" and "complete" callback are deprecated. Instead you should be using "done", "fail" and "always".
从 jQuery 1.8 开始,不推荐使用“success”、“error”和“complete”回调。相反,您应该使用“完成”、“失败”和“始终”。
So you could have:
所以你可以有:
function checkUserIdExists(userid, callback) {
return $.ajax({
url: 'theurl',
type: 'GET',
cache: false,
data: {
userid: userid
}
})
.done(callback)
.fail(function(jqXHR, textStatus, errorThrown) {
// Handle error
});
}
checkUserIdExists(2, function(data) {
console.log(data); // Do what you want with the data returned
});
回答by jmort253
This isn't how JavaScript asynchronous programming was really meant to be done. Instead, use a callback in your success function to call another function to use your data returned from the server.
这并不是 JavaScript 异步编程的真正目的。相反,在成功函数中使用回调来调用另一个函数以使用从服务器返回的数据。
回答by Olaseni
Tim, the two scenarios are mutually exclusive; an asynchronous operation will not serve any purpose for, nor will it be able to retrieve returned data.
蒂姆,这两种情况是相互排斥的;异步操作不会用于任何目的,也不能检索返回的数据。
You should look at an event-enabled infrastructure for your ajax calls
您应该为 ajax 调用查看启用事件的基础设施

