当前位置: 代码迷 >> Java Web开发 >> JS window.location.href 跳转失败解决办法
  详细解决方案

JS window.location.href 跳转失败解决办法

热度:1450   发布时间:2016-04-16 22:06:59.0
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";  
      
                }
  相关解决方案