0
我检查了返回字符串是“已添加”或“已用”或“假”,但每次都执行document.write(theresult)的最后一条语句。什么是问题,我期待如果返回字符串是“使用”然后警报“1”。请帮助,非常感谢。Ajax返回字符串在if语句中意外的结果
function add_member()
{
var xmlhttp;
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)
{
// document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
var theresult = xmlhttp.responseText;
if (theresult == "used") {
alert("1");
}
else if (theresult == "added") {
alert("2");
}
else if (theresult == "false") {
alert("3");
}
else
{
document.write(theresult);
}
}
}
xmlhttp.open("GET","add.php?addnew=true&email=" + document.memberform.email.value + "&tel=" + document.memberform.tel.value,true);
xmlhttp.send();
}
在这种情况下添加快速跟踪线以进行调试总是明智的。在上面的例子中,你可以做的是,在'var theresult = xmlhttp.responseText'行后面,检查'theresult'包含的内容。你可以使用'alert(“[”+ theresult +“]”)'或'console.log(“[”+ theresult +“]”)''。我添加了这些方括号,因为它们可以帮助您在出现额外空白导致程序失败的情况下进行调试。另外,处理非结构化数据很少是一件好事。 – 2012-02-15 05:14:23