【发布时间】:2010-06-02 09:05:27
【问题描述】:
我有一个函数用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
【问题讨论】:
-
失败怎么办?
request在哪里声明和设置? -
已贴花且设置良好。这里没什么好担心的。看起来不像是范围问题?
标签: javascript ajax