从JavaScript中的嵌套函数返回 [英] Returning from a nested function in javascript
本文介绍了从JavaScript中的嵌套函数返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
code:
$ b $ 我试图做一个使用jquery的ajaxfunction从我的ajax.php文件获取信息的函数。 b 函数ajaxIt(dataLine){
$ .ajax({
类型:POST,
url:ajax .php,
data:ajax = true&+ dataLine,
success:function(msg){
console.log([AjaxIt]:+ dataLine +returned+ msg);
return msg;
}
});
if(ajaxIt(action = loggedIn)==1){
console.log(Logged In);
loggedIn = true;
initiate2();
}
问题是我无法获得成功函数以返回所有通往ajaxIt函数的途径。任何人都可以点亮我如何做这样的事情?
谢谢。
解决方案
您需要调用回调
函数来处理数据:
function ajaxIt(dataLine,cb){
$ .ajax({
type:POST,
url:ajax.php,
data :ajax = true&+ dataLine,
success:function(msg){
if($。isFunction(cb))
cb.apply(null,[msg]);
}
});
ajaxIt(action = loggedIn,function(data){
if(data ===1){
console.log(登录);
loggedIn = true;
initiate2();
}
});
I'm trying to make a function that uses jquery's ajaxfunction to get some info from my ajax.php file.
code:
function ajaxIt(dataLine){
$.ajax({
type: "POST",
url: "ajax.php",
data: "ajax=true&"+dataLine,
success: function(msg){
console.log("[AjaxIt]: "+dataLine+" returned "+msg);
return msg;
}
});
}
if(ajaxIt("action=loggedIn")=="1"){
console.log("Logged In");
loggedIn=true;
initiate2();
}
The problem is that I can't get the success function to return all the way to the ajaxIt function. Could anyone shed some light onto how I could do something like that?
Thanks.
解决方案
You need to invoke a callback
function to process data that way:
function ajaxIt(dataLine, cb){
$.ajax({
type: "POST",
url: "ajax.php",
data: "ajax=true&"+dataLine,
success: function(msg){
if($.isFunction(cb))
cb.apply(null, [msg]);
}
});
}
ajaxIt("action=loggedIn", function(data){
if(data === "1"){
console.log("Logged In");
loggedIn=true;
initiate2();
}
});
这篇关于从JavaScript中的嵌套函数返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文