Ali*_*aly 4 javascript ajax jquery
我有一个方法可以发送AJAX请求并返回一个指示JSON stringTokens 记录的结果,我试图获取这个结果并将它分配给一个被调用的全局变量tokens,然后在其他函数中重用这个全局变量。
我将结果分配给该变量并将其记录到控制台,如下所示:
var tokens = null;
function PopulateAllTokens() {
$.ajax({
type: "POST",
url: "NewToken.aspx/PopulateAllTokens",
contentType: "application/json; charset=utf-8",
dataType: "json",
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("Request: " + JSON.stringify(XMLHttpRequest) + "\n\nStatus: " + textStatus + "\n\nError: " + errorThrown);
},
success: function (result) {
tokens = JSON.parse(result.d);
console.log(tokens);
populateTokensToTable(tokens);
}
});
}
Run Code Online (Sandbox Code Playgroud)
问题是,当我将结果分配给变量然后将其记录到控制台时,它成功地显示了结果,但是当我稍后想在其他函数中重用它时,它显示变量仍然是null.
例如,我想在以下jQuery代码中使用它,但它显示该变量为空:
$("#example").DataTable({
"columns": getTokens(),
"data": tokens
});
Run Code Online (Sandbox Code Playgroud)
只是为了澄清变量和函数都在内部定义:
$(document).ready(function () {//function and var}
请问有什么建议吗?
Most of the time problems arise when you try to get a value via AJAX and then try to use that value outside the whole $.ajax() construct. The reason is that responses from async calls can only be accessed safely inside their success or complete callbacks - there is no guarantee the value will be populated before either of those callbacks complete. To work around this, you can either move all following code to be called from inside the AJAX callback, or wait for your global vars to be set by the callback.
var tokens = null;
function PopulateAllTokens() {
$.ajax({
type: "POST",
url: "NewToken.aspx/PopulateAllTokens",
contentType: "application/json; charset=utf-8",
dataType: "json",
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("Request: " + JSON.stringify(XMLHttpRequest) + "\n\nStatus: " + textStatus + "\n\nError: " + errorThrown);
},
success: function (result) {
tokens = JSON.parse(result.d);
console.log(tokens);
populateTokensToTable(tokens);
EverythingElseYouWantToRun();
}
});
}
function EverythingElseYouWantToRun() {
// you can do whatever you want with the response here
$("#example").DataTable({
"columns": getTokens(),
"data": tokens
});
}
Run Code Online (Sandbox Code Playgroud)
var tokens = null;
function PopulateAllTokens() {
$.ajax({
type: "POST",
url: "NewToken.aspx/PopulateAllTokens",
contentType: "application/json; charset=utf-8",
dataType: "json",
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("Request: " + JSON.stringify(XMLHttpRequest) + "\n\nStatus: " + textStatus + "\n\nError: " + errorThrown);
},
success: function (result) {
tokens = JSON.parse(result.d);
console.log(tokens);
populateTokensToTable(tokens);
}
});
}
function RunWhenVariableIsPopulated(variable, callback, timeout) {
if (variable === null) {
setTimeout(function() {
RunWhenVariableIsPopulated(variable, callback, timeout);
}, timeout);
} else {
callback(variable);
}
}
Run Code Online (Sandbox Code Playgroud)
Then later you can call:
RunWhenVariableIsPopulated(tokens, function() {
$("#example").DataTable({
"columns": getTokens(),
"data": tokens
});
}, 400 /* or whatever your average round-trip time is*/);
Run Code Online (Sandbox Code Playgroud)
Beware: this can cause the browser to hang noticeably if your AJAX response time is really long, and effectively turns an async call into a synchronous one. Hope this helps with your current situation!
| 归档时间: |
|
| 查看次数: |
9639 次 |
| 最近记录: |