2017-03-06 85 views
0

我有一个登录页面。当用户输入用户名和密码,然后单击登录按钮时,该按钮将调用一个Ajax XmlHttpRequest,它将用户名和密码发送到process.php。 php处理数据,并在用户名或密码不正确时返回。但是,如果正确,我想将用户重定向到session.php。 Php头文件不起作用,因为它需要页面代码,并且JavaScript不起作用,因为它不运行代码。我该怎么做?PHP将用户的另一个页面重定向到下载的Ajax XmlHttpRequest中

这里是阿贾克斯:

function logIn() { 
    var loader = document.getElementById('loaderParent'); 
    var form = document.getElementById('formI'); 
    form.style.display = "none"; 
    loader.style.display = "block"; 
    setTimeout(function() { 
     var xhttp = new XMLHttpRequest(); 
     var username = document.getElementById('username').value; 
     var password = document.getElementById('password').value; 
     xhttp.onreadystatechange = function() { 
     if (this.readyState == 4 && this.status == 200) { 
     // console.log(4); 
      document.getElementById("form").innerHTML = 
      this.responseText; 
     } 
     }; 
     xhttp.open("POST", "class/process.php", true); 
     xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
     xhttp.send("zone=LogIn&section=2&username="+username+"&password="+password); 
    }, 500); 
} 
+1

显示您的代码.... – Naincy

+0

我们需要查看您的代码,但结果是您必须在AJAX函数的成功回调中使用JavaScript执行重定向。 –

+0

我附上了代码 – MGamer333

回答

0

你需要的是一个JavaScript重定向。例如,用这个来重定向到谷歌主页:

location.href = 'http://www.google.com'; 

顺便说一句,我推荐你使用一个库如jQuery简化您的Ajax调用。

相关问题