jQuery 从ajax调用请求返回值的函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15847292/
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
function that return a value from ajax call request
提问by Mohit Pandey
I want a function that returns a value from an ajax request. I want to stop javascript execution until the function get its value which is return by ajax asynchronous request. Something like:
我想要一个从 ajax 请求返回值的函数。我想停止 javascript 执行,直到函数获得它的值,该值由 ajax 异步请求返回。就像是:
function myFunction() {
var data;
$.ajax({
url: 'url',
data: 'data to send',
success: function (resp) {
data = resp;
},
error: function () {}
}); // ajax asynchronus request
return data; // return data from the ajax request
}
回答by su-
You need to register a callback function, something like this:
您需要注册一个回调函数,如下所示:
function test() {
myFunction(function(d) {
//processing the data
console.log(d);
});
}
function myFunction(callback) {
var data;
$.ajax({
url: 'url',
data: 'data to send',
success: function (resp) {
data = resp;
callback(data);
},
error: function () {}
}); // ajax asynchronus request
//the following line wouldn't work, since the function returns immediately
//return data; // return data from the ajax request
}
回答by thumber nirmal
Ajax is asynchronous, that means that the ajax call is dispatched but your code keeps on running as happy as before without stopping. Ajax doesn't stop/pause execution until a response is received. You'll have to add an extra callback function or something like that.
Ajax 是异步的,这意味着 ajax 调用被分派,但您的代码继续像以前一样愉快地运行而不会停止。在收到响应之前,Ajax 不会停止/暂停执行。你必须添加一个额外的回调函数或类似的东西。
function myFunction() {
var data;
$.ajax({
url: 'url',
data: 'data to send',
async: false,
success: function (resp) {
data = resp;
callback.call(data);
},
error: function () {}
}); // ajax asynchronus request
return data; // return data from the ajax request
}
回答by ravisolanki07
you need to do asyn = False like :
你需要做 asyn = False 像:
$.ajax({
async: false,
// ...
success: function(jsonData) {
//Your Logic
}
});
回答by Ryan
$.ajax({
url: 'url',
data: 'data to send',
async: false,
success: function (resp) {
data = resp;
},
error: function () {}
}); // ajax synchronus request
回答by Eli
Use async: false
for your ajax request since Ajax is asynchronous.
使用async: false
以来的Ajax您的Ajax请求是异步的。
Setting async to false means that the statement you are calling has to complete before the next statement in your function can be called. If you set async: true then that statement will begin it's execution and the next statement will be called regardless of whether the async statement has completed yet.
将 async 设置为 false 意味着您正在调用的语句必须在调用函数中的下一个语句之前完成。如果您设置 async: true ,则该语句将开始执行,并且无论 async 语句是否已完成,都将调用下一条语句。
From jQuery docs:
来自 jQuery文档:
By default, all requests are sent asynchronously (i.e. this is set to true by default). If you need synchronous requests, set this option to false
默认情况下,所有请求都是异步发送的(即默认设置为 true)。如果您需要同步请求,请将此选项设置为 false