如何在纯Javascript中的PHP AJAX调用中使用头重定向?

如何在纯Javascript中的PHP AJAX调用中使用头重定向?,javascript,php,ajax,Javascript,Php,Ajax,我正在创建一个登录表单,我需要将用户重定向到他/她的个人资料页面!我使用的是AJAX请求,所以头重定向根本不起作用。它只是停留在主页上那么如何在纯javascript PHP ajax调用中将用户重定向到另一个页面?请用纯javascript给出答案。我根本不喜欢使用jQuery Javascript: function ajaxCall(){ var xhttp; if(window.XMLHttpRequest){ xhttp = new XMLHttpRe

我正在创建一个登录表单,我需要将用户重定向到他/她的个人资料页面!我使用的是AJAX请求,所以头重定向根本不起作用。它只是停留在主页上

那么如何在纯javascript PHP ajax调用中将用户重定向到另一个页面?请用纯javascript给出答案。我根本不喜欢使用jQuery

Javascript:

function ajaxCall(){
    var xhttp;

    if(window.XMLHttpRequest){
        xhttp = new XMLHttpRequest();
    }

    xhttp.onreadystatechange = function(){
        if(this.readyState === 4 && this.status === 200){
            document.getElementById('error').innerHTML = this.responseText;
        }
    };

    var parameters = 'email='+document.getElementById('email')+'&password='+document.getElementById('password');
    xhttp.open('POST', 'login.php', true);
    xhttp.setRequestHeader('Content-type', 'application/x-www/form/urlencoded');
    xhttp.send(parameters);
}
Login.php(php)


如果这个法案行得通,试试看

  window.location.replace("http://www.google.com");
打一个ajax电话

<?php
header('Content-Type: application/json');
echo json_encode(['location'=>'/user/profile/']);
exit;
?>
在ajax成功javascript函数中

 xhr.onreadystatechange = function () {
            if (xhr.status >= 200 && xhr.status <= 299)
            {
                var response = JSON.parse(xhr.responseText);
                if(response.location){
                  window.location.href = response.location;
                }
            } 

    }
xhr.onreadystatechange=函数(){

如果(xhr.status>=200&&xhr.status我来到这里是为了寻找Ajax解决方案,但Montrealdovone的回答提供了有用的信息。如果其他人也对Ajax方法感兴趣,请看:

$('body').on('submit', '#form_register', function (e) {
    var form = $(this);
    $.ajax({
          type: 'POST',
          url: 'form_submissions.php',
          data: form.serialize() + "&submit",
          dataType:"json",
              success: function (data) {
              if(data.location){
                window.location.href = data.location;
              }
          },
          error: function(data) {
              alert("Error!");
          }
      });
    e.preventDefault();
});


就我的2美分,希望有帮助:)

你能展示一下你目前为止写的代码吗?header('Location:profile.php'))在AJAX中根本不起作用!有没有其他方法可以在纯javascript中重定向用户!啊!好吧!@BluefirePossible重复@Bill在什么意义上不起作用?是否有错误消息?不,我不是在谈论javascript重定向。我需要PHP头重定向,它在AJAX调用中不起作用!@Bill在您的AJAX代码中尝试一下,当你需要重定向,如果它的工作重定向头。谢谢你的回答,兄弟!但得到了一个好的来自@MontrealDevOne@BillRoger Captan:)这不是jQuery的成功吗?如果不是的话,我应该如何在Javascript AJAX中使用?非常感谢兄弟!我只是想不出你的解决方案。但真的非常感谢!标记为回答:)添加&&xhr.status<300?如果不是200,则返回false。这完全有助于更好地理解最重要的答案-欣赏它,巴德
 xhr.onreadystatechange = function () {
            if (xhr.status >= 200 && xhr.status <= 299)
            {
                var response = JSON.parse(xhr.responseText);
                if(response.location){
                  window.location.href = response.location;
                }
            } 

    }
$('body').on('submit', '#form_register', function (e) {
    var form = $(this);
    $.ajax({
          type: 'POST',
          url: 'form_submissions.php',
          data: form.serialize() + "&submit",
          dataType:"json",
              success: function (data) {
              if(data.location){
                window.location.href = data.location;
              }
          },
          error: function(data) {
              alert("Error!");
          }
      });
    e.preventDefault();
});