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.

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

报告相同问题?

悬赏问题

  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败