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 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置