我有一个登录页面。当用户输入用户名,密码并单击“登录”按钮时,该按钮将调用Ajax XmlHttpRequest,该用户将用户名和密码发送到process.php。 php处理数据,如果用户名或密码不正确则返回。但是如果正确的话,我想将用户重定向到session.php。 Php标头不起作用,因为它占用页面代码,JavaScript也不起作用,因为不运行代码。我该怎么办?
这是Ajax:
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§ion=2&username="+username+"&password="+password);
}, 500);
}
答案 0 :(得分:0)
你需要的是一个javascript重定向。例如,使用此选项重定向到Google主页:
location.href = 'http://www.google.com';
顺便说一下,我建议你使用像JQuery这样的库来简化你的Ajax调用。