jquery ajax返回:未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4580265/
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
jquery ajax return: undefined
提问by Nightbox
I'm sure you know this problem, I'm still trying to solve it for few days. I've tried lots of stuff but no one worked:
我相信你知道这个问题,我还在努力解决它几天。我尝试了很多东西,但没有一个成功:
Here is the code
这是代码
function lobbyLeader() {
$.ajax({
data: {"id": 1, "request": "lobbyinfo", "method": "read"},
url: 'api.php',
dataType: 'json',
success: function(data){
result = data.leader;
return result;
}
});
}
alert(result);
will show 1
but when using in an other function it says undefined
.
alert(result);
会显示,1
但在其他函数中使用时,它会显示undefined
.
回答by Nick Craver
You can't return
from an asynchronous function like this, you're returning from that success
callback function, not the parent function. Instead, kick off whatever you need in the callback, like this:
你不能return
从这样的异步函数中返回,你是从那个success
回调函数返回的,而不是父函数。相反,在回调中启动您需要的任何内容,如下所示:
function lobbyLeader() {
$.ajax({
data: {"id": 1, "request": "lobbyinfo", "method": "read"},
url: 'api.php',
dataType: 'json',
success: function(data){
someOtherFunc(data.leader);
}
});
}
回答by lonesomeday
The problem here is that AJAX is asynchronous (it's what the first A stands for). This means that the function returns immediately; the success
handler is only called when the request is successful. This means that lobbyLeader
returns immediately after you have made your request, so returns nothing.
这里的问题是 AJAX 是异步的(这是第一个 A 代表的意思)。这意味着函数立即返回;在success
当请求是成功的处理程序只调用。这意味着lobbyLeader
在您提出请求后立即返回,因此不返回任何内容。
If you have any code that needs to run after you have received the data, it must be placed in the success
handler (or another AJAX event handler) or be invoked by it.
如果您有任何代码需要在收到数据后运行,则必须将其放置在success
处理程序(或另一个 AJAX 事件处理程序)中或由它调用。
回答by matt
Correctly stated that you can't really return from the Success, however, if you are doing what I think you are doing, and that is to grab the data and return it and assign it to something in another area IE
正确地说你不能真正从成功中返回,但是,如果你正在做我认为你正在做的事情,那就是获取数据并将其返回并将其分配给另一个区域中的某些东西 IE
var obj = lobbyLeader();
var obj = 大堂领导();
Then you can just set the function to async:false and return the obj outside of the Success after the code has finished running. example
然后,您可以将该函数设置为 async:false 并在代码运行完成后在 Success 之外返回 obj。例子
function lobbyLeader() {
var obj;
$.ajax({
async:false;
data: {"id": 1, "request": "lobbyinfo", "method": "read"},
url: 'api.php',
dataType: 'json',
success: function(data){
obj= JSON.parse(data);
}
});
return obj;
}
This way the script stops to wait for success and then sets and returns.
这样脚本停止等待成功,然后设置并返回。
回答by mass
I recommend this way.
我推荐这种方式。
- use the ajax call by the async:false.
- move the return statement after the ajax call.
- 通过 async:false 使用 ajax 调用。
- 在 ajax 调用之后移动 return 语句。
Example :
例子 :
function makeJQGridDataFromList(url)
{
var rowData;
var viewPage = 0;
var viewTotal = 0;
var viewRecords = 0;
var resultObject;
$.ajax({
type:"GET",
url:url,
async: false,
success:function(args){
if(args.result==true)
{
try
{
viewPage = args.cond.pageIndex;
viewTotal = args.cond.recordCountPerPage;
viewRecords = args.cond.totalCnt;
rowData = jsonMakeRowsForGrid(args.data);
}
catch (e)
{
console.debug("Error!");
alert("Invalid data");
return;
}
} else
{
alert("API return ERROR!");
return;
}
},
error:function(e){
alert("Fail AJAX communication");
return;
}
});
resultObject = {
page : viewPage,
total : viewTotal,
records : viewRecords,
rows : rowData
};
return(resultObject);
}
You can test the following method.
您可以测试以下方法。
(In the other file (html or js))
(在另一个文件(html 或 js)中)
var gridData = makeJQGridDataFromList(openAPIUrl);
console.debug(">> " + JSON.stringify(gridData));
You can see the gridData.
您可以看到 gridData。
I faced same problems. :)
我遇到了同样的问题。:)
回答by Jacob Relkin
When you return
values from within an anonymous function that is executed asynchronously, those values will not propogate up the scope chain, the return
statement is only applied on the current function scope, not the surrounding scope, $.ajax()
is asynchronous, so immediately after executing the statement, the outer function returns, so there is no return value of the outer function, that's why you're getting undefined
.
当您return
从异步执行的匿名函数中取值时,这些值不会向上传播作用域链,该return
语句仅应用于当前函数作用域,而不是周围的作用域,$.ajax()
是异步的,因此在执行该语句后,外部函数返回,所以外部函数没有返回值,这就是为什么你得到undefined
.
The only way to hook into a possible return value from the callback function passed to $.ajax
is to invoke another outer function, passing in the desired data.
从传递给的回调函数中挂钩到可能的返回值的唯一方法$.ajax
是调用另一个外部函数,传入所需的数据。