我在调用ajax之后调用一个回调函数,如下所示。
var mypage= {};
var mypagebox = {
somefunction: function() {
var self = this;
self.createID(obj, function(resp) {
if (resp.ERRORSTATUS == 0) {
mypage.newID = resp.newID;
console.log("pooped in array ");
console.log(mypage.newID);
}
console.log("outside if ");
console.log(newID.newID);
});
console.log("outside scope ");
console.log(mypage.newID);
},
createID: function(_obj, callback) {
$.ajax({
headers: {
'Content-Type': "application/json; charset=utf-8",
'dataType': "json"
},
data: {
//Some data
},
url: "/url"
}).done(function(res, d) {
callback(JSON.parse(res));
});
}
};

因此,当调用mypagebox.somefunction时,我在控制台中获得以下输出。
outside scope
[]
outside total scope
[]
pooped in array
[Object]
outside if
[Object]
当我尝试在mypagebox的另一个方法中使用mypage.newID时,我得到null。我该如何解决这个问题。
答案 0 :(得分:0)
您的代码和解释不是我们的帮助。你可以组织someFunction(),newID对象是什么? console.log(newID.newID)??
我建议你应该在jsfiddle平台上添加项目。