在函数和对象之外设置变量
问题描述:
我试图设置变量result
,但它仍然未定义。在函数和对象之外设置变量
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
if(xmlhttp.responseText)
{
result = true
alert(xmlhttp.responseText+" here1")
}
else
{
document.getElementById("report").innerHTML = "wrong password/username"
alert(xmlhttp.responseText+" here2")
result = false
}
}
}
xmlhttp.open("POST", "process.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("name="+encodeURIComponent(name.value));
alert("final value: "+result)
return result//always undefined
如何让对象影响其所在函数以外的变量?这个问题比我更容易理解。当用户尝试提交表单时调用此函数。如果返回true,则应提交表单,但如果返回false,则不应提交表单。我现在有代码(感谢 sweetamylase)
var result
var checkResult = function(result) {
alert("final value: " + result);
return result
};
xmlhttp.onreadystatechange=function()
{
var result = null;
if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
if(xmlhttp.responseText)
{
result = true
alert(xmlhttp.responseText+" here1")
}
else
{
document.getElementById("report").innerHTML = "wrong password/username"
alert(xmlhttp.responseText+" here2")
result = false
}
checkResult(result); // pass result to another function
}
}
xmlhttp.open("POST", "process.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("name="+encodeURIComponent(name.value));
但形式上总是提交,甚至在“终值...”显示。如果我在代码的末尾添加返回false,那么表单永远不会被提交。
答
使用callback
function callServer(callback){
if (window.XMLHttpRequest)
------------------- code ------------
xmlhttp.open("POST", "process.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("name="+encodeURIComponent(name.value));
xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
callback(xmlhttp.responseText);
}
}
}
}
答
的“onreadystatechange的”功能assynchronous,这就是为什么你设置时被触发时要执行的回调函数。 你在做什么对javascript说:“当你改变你的就绪状态时,就这样做。” 然后你的代码继续运行e返回结果的值(这是不确定的,因为就绪状态没有改变)
所以,如果你需要使用结果的值,你应该在你的回调函数。
希望它可以帮助..
答
AJAX是异步所以你需要修改你的onreadystatechange()
功能,您可以通过result
另一个函数来处理你的逻辑:
var checkResult = function(result) {
alert("final value: " + result);
/* handle your result here */
};
...
xmlhttp.onreadystatechange=function()
{
var result = null;
if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
if(xmlhttp.responseText)
{
result = true
alert(xmlhttp.responseText+" here1")
}
else
{
document.getElementById("report").innerHTML = "wrong password/username"
alert(xmlhttp.responseText+" here2")
result = false
}
checkResult(result); // pass result to another function
}
}
xmlhttp.open("POST", "process.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("name="+encodeURIComponent(name.value));
AJAX是异步* *!这意味着返回结果;在你的AJAX调用完成之前运行*。 'xmlhttp.send'在后台运行,脚本继续。所有与'result'有关的工作都是在回调中完成的。 – 2013-04-04 19:15:46
使用回调或使xmlhttp请求同步。 – Anoop 2013-04-04 19:17:11