[英]How to get return value from AJAX in requireJS?
I structured my code with requireJS. 我使用requireJS构建了代码。 In my init.js
在我的init.js中
require(["jquery", "user"], function($, user){
console.log(user.isLogin());
});
In my user.js 在我的user.js中
define(["jquery"], function($){
return{
isLogin: function(){
var status = 0;
function callApi(){
return $.ajax({
type: "post",
url: urls.api_isLogin,
dataType: "json"
}
callApi().done(function(data){
status = data.data;
console.log(status);
return status;
});
}
};
});
I can get the data from ajax call: 我可以从ajax调用获取数据:
console.log(status);
//output: true
//means I can get the value from the ajax call
But how can I pass it into init.js? 但是如何将其传递给init.js?
I got "undefined" from: 我从以下地方得到“未定义”:
console.log(user.isLogin());
OK. 好。 I figured out by myself.
我自己想通了。
define(["jquery"], function($){
return{
isLogin: function(){
return $.ajax({
type: "post",
url: urls.api_isLogin,
dataType: "json"
}
}
};
});
Then in another file: 然后在另一个文件中:
$.when(user.isLogin()).then(function(data){
console.log(data.data);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.