doudichu1358 2016-11-13 15:47
浏览 40
已采纳

我使用XMLHttpRequest成功验证表单后如何使用移动到另一个页面,因为Header函数不能与ajax一起使用

Javascript:

var xhttp = new XMLHttpRequest();

xhttp.onreadystatechange = function(){
    if(xhttp.readyState==4 && xhttp.status==200){
        document.getElementById("display_result").innerHTML=xhttp.responseText;
    }
};
document.getElementById('loading').style.display='block';
xhttp.open("POST", "users_process.php", true);

xhttp.setRequestHeader('X-Alt-Referer', 'dashboard.php');
xhttp.send(data);

PHP:

<?php
    $execute_query=$data_access->Execute($query);
    if($execute_query==true){
        header("location: dashboard.php");
    }else{
        echo("<div id='error'>Some error occured during uploading Please try again</div>");
    }
?>
  • 写回答

1条回答 默认 最新

  • dpymrcl269187540 2016-11-13 15:59
    关注

    XMLHttpRequest returns a DOMString that contains the response to the request as text, or null if the request was unsuccessful or has not yet been sent.

    So when you redirect on the serverside, you don't redirect on the client side; the request just returns whatever is echoed on the server side as response. What you can do is return a value (a string or a boolean) assuring that the query was OK. Then, on the client side, you can check for that value and perform a redirect using window.location.href.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?