Ajax不起作用啊!!求大神解答

qq_27678291 2015-12-25 09:16:12
function fun(){
var req=null;
if(window.XMLHttpRequest){
req=new XMLHttpRequest();
}else{
req=new AcitveXObject("Msxml2.XMLHTTP");
}
alert("aaa");
req.open("post","servlet/UserServlet?method=ajax");
alert("bbb");
req.onreadystatechange = function(){
if(req.readyState==4 && req.status == 200){
alert("ccc");
if(req.responseText>0){
alert("用户名已经存在");
}else if(req.responseText=-2){
alert("bunengweik");
}
}
alert("ddd")
}
alert("eee")
req.setRequestHeader("content-type"," application/x-www-form-urlencoded");
var txt = document.getElementById("username");
req.send("name=" + txt.value);
}

只能弹出aaa bbb eee 怎么回事啊
...全文
119 3 打赏 收藏 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
应该和1#说的一样,你id打错了,导致获取到的txt是null,调用value属性报错了导致没有send发送请求,从而状态转换函数没有被调用
wuheng_1 2015-12-26
  • 打赏
  • 举报
回复
req.open调用少了个参数,检查一下吧。
slwsss 2015-12-25
  • 打赏
  • 举报
回复
txt 这个有值吗

52,778

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧