ajax下 location.href不跳转,JS window.location.href 跳转失败解决办法

JS window.location.href 跳转失败

用Ajax做了一个登录跳转的功能,ajax验证通过了,但是却没有跳转,不知道怎么回事,请各位高手帮忙看下,感激不尽!

function judge(){

result = verify();

var name = document.getElementById("user").value;

var pwds = document.getElementById("pwd").value;

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");

}//end if()

xmlhttp.onreadystatechange=function(){

//alert("ajax状态码:"+xmlhttp.readyState);

//alert("http状态码:"+xmlhttp.status);

//if (xmlhttp.readyState==4 && xmlhttp.status==200)//本地测试的时候,status属性不管是在“成功”还是“页面未找到”的情况下,都返回的是0,而不是200和404

if (xmlhttp.readyState==4 && xmlhttp.status==200 ) {

//alert("xmlhttp.responseText == "+xmlhttp.responseText);

if(xmlhttp.responseText=="right"){

alert("success!");

//window.location.href="http://localhost/lee/yxlego/view/picture.php";

location.href="http://www.baidu.com/";

//setTimeout('这是延迟3秒之后显示的!',3000);

}else{

alert("登录信息有误,请核对后再次登录!");

}//end if()

}//end if()

}//end func

//xmlhttp.open("GET","../control/login_contr.php?name="+name+"&pwd="+pwds+"&t="+ Math.random(),true);//异步处理(第三个参数为true)时Ajax状态码总是为1,有问题

xmlhttp.open("GET","../control/login_contr.php?name="+name+"&pwd="+pwds+"&t="+ Math.random(),false);

xmlhttp.send();

}//end func judge()

------解决方案--------------------

当您使用 async=false 时,请不要编写 onreadystatechange 函数 - 把代码放到 send() 语句后面即可xmlhttp.open("GET","test1.txt",false);

xmlhttp.send();

document.getElementById("myDiv").innerHTML=xmlhttp.responseText;

document.getElementById("myDiv")是一个DIV的ID名你这样就OK了

------解决方案--------------------

var req; //定义变量,用来创建xmlhttprequest对象

function creatReq() // 创建xmlhttprequest,ajax开始

{

var name = document.getElementById("user").value;

var pwds = document.getElementById("pwd").value;

var url="../control/login_contr.php?name="+name+"&pwd="+pwds+"&t="+ Math.random();

if(window.XMLHttpRequest) //非IE浏览器及IE7(7.0及以上版本),用xmlhttprequest对象创建

{

req=new XMLHttpRequest();

}

else if(window.ActiveXObject) //IE(6.0及以下版本)浏览器用activexobject对象创建,如果用户浏览器禁用了ActiveX,可能会失败.

{

req=new ActiveXObject("Microsoft.XMLHttp");

}

if(req) //成功创建xmlhttprequest

{

req.open("GET",url,true); //与服务端建立连接(请求方式post或get,地址,true表示异步)

req.onreadystatechange = callback; //指定回调函数

req.send(null); //发送请求

}

}

function callback() //回调函数,对服务端的响应处理,监视response状态

{

if(req.readystate==4) //请求状态为4表示成功

{

if(req.status==200) //http状态200表示OK

{

alert("success!");   //这里可以执行的,就是下面的那句没有跳转

location.href="http://localhost/lee/yxlego/view/picture.php";

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值