我有一个函数,它用var关键字声明一个变量。然后它启动一个 AJAX 请求来设置变量的值,然后这个变量从函数中返回。
但是,我的实施失败了,我不知道为什么。
这是代码的简化版本;
function sendRequest(someargums) {
/* some code */
var the_variable;
/* some code */
request.onreadystatechange =
//here's that other function
function() {
if (request.readyState == 4) {
switch (request.status) {
case 200:
//here the variable should be changed
the_variable = request.responseXML;
/* a lot of code */
//somewhere here the function closes
}
return the_variable;
}
var data = sendRequest(someargums); //and trying to read the data I get the undefined value