这个问题的标题不是很清楚,但我无法解释我在问什么而没有提供一些代码。Javascript - 从其他地方调用的函数返回值
我正在制作一个使用AJAX提交报表(这是数组)的页面。我有存储在库函数该阵列提交给PHP页面,如下图所示:
function saveReport(params)
{
var url="reports/save.php";
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
return false;
}
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("POST",url,true);
//Send the proper header information along with the request
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-length", params.length);
xmlHttp.setRequestHeader("Connection", "close");
xmlHttp.send(params);
}
function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
if (xmlHttp.responseText == 'complete')
{
return true;
}
else
{
return false;
}
}
}
(我不相信GetXmlHttpObject()函数有关的问题)
我会希望能够从我的页面调用saveReport()这样的
success = saveReport(params);
if (success == true)
{
alert('success');
}
else
{
alert('failure');
}
因此函数saveReport()需要返回其从功能stateChanged()被调用时,这样的页面返回的值那叫saveReport()可以决定做什么 - 可能在不同的页面上有所不同。有没有办法做到这一点?
我意识到这可能是我尝试的方式不可能的,但是在那里我可以做同样的效果。
我没有发现这个。我使用匿名函数传递给使用jQuery的函数,这些函数在成功或失败时被调用。 – 2012-02-22 17:47:18