使用AJAX的返回值跟字符串进行比较,无论返回的结果是否与字符串相同都是false

问题出在

var txt = new String(xhr.responseText);

//获得一个 xml dom 对象。

if("no"==txt1){

alert("no");

}else{

alert("yes");

}

//dom 操作、更新页面

}

下面是具体的代码

function query(){

var xhr = getXmlHttpRequest();

xhr.open('post','servlet/RegistAction',true);

//必须添加一个消息头 content-type

xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

xhr.onreadystatechange=function(){

//编写相应的处理代码

if(xhr.readyState == 4){

//只有 readyState 等亍 4,xhr 才完整地接收到了服务器返回的数据。

//获得文本数据

var txt = new String(xhr.responseText);

//获得一个 xml dom 对象。

if("no"==txt1){

alert("no");

}else{

alert("yes");

}

//dom 操作、更新页面

}

};

xhr.send('username='+document.getElementById("username").value);

}

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐