繁体   English   中英

来自 Ajax 调用的 PHP 重定向

[英]PHP redirect from Ajax Call

我使用 Ajax(使用本机 JavaScript)和 php 的组合来构建登录。

from 位于一个 php 文件 (login.php) 中。 当它被提交时,它运行一个 JS onlclick 函数,该函数将表单数据发布到另一个验证表单数据的 php 文件:

<input type="button" name="submit" value="Submit" onclick="JavaScript:xmlhttpPost('validator.php')"/>

使用 JavaScript 在 div 中返回来自 validator.php 的结果:

function updatepage(str){

document.getElementById("result").innerHTML = str;

}

最后,如果密码和用户名都正确,validator.php 会像这样运行重定向:

if ( // form data is valid ) {

    header("Location: welcome.php");

}

但是,因为一切都通过 ajax 运行,这会导致“welcome.php”页面显示在原始登录页面的“结果”div 中。

有没有办法通过 JavaScript 发送重定向?

您可以使用以下代码通过 JavaScript 重定向:

window.location.href = 'welcome.php';

当您进行 AJAX 调用时,您不能通过像 PHP 这样的服务器端应用程序进行重定向。

您必须获得响应并在您的 JavaScript 中完成。

这是一个使用 jQuery 和 PHP 的 ajax 请求示例。

将此代码放在您的前端(视图)上:

<script type="text/javascript">
    $(document).ready(function(){
        $.ajax({
            url: 'your-php-file.php',
            dataType: 'html',
            success: function(response) {
                window.location.href = response;
            }
        });
    });
</script>

https://api.jquery.com/jQuery.ajax/

在你的 PHP 文件中,在你做了你想做的事情之后; echo 要重定向的路径或文件作为响应。

<?php
    echo "file-or-path-to-redirect.php";

代替实际有效的答案,我想出了一个可行的解决方案。 我不确定这是否是正确的做法,但效果很好。

在我的 validator.php 中,当表单值正确时,我输入以下内容:

if ( // form data is valid ) {

    echo 'redirect';

}

然后,在我的登录页面上,当从 php 页面返回字符串时,我把这个:

function updatepage(str){

   if (str.match(/redirect/)) {
      window.location.replace('welcome.php');
   }
   else {
      document.getElementById("result").innerHTML = str;
   }
}

这个想法是,当validator.php 确认登录凭据正确时,它会返回一个字符串。

如果该字符串与“redirect”匹配,JavaScript 将重定向页面。

如果有人对此有任何意见,请发表评论。 我觉得这是一个很好的解决方案。 很惊讶我之前没有想到。

window.location.href='welcome.php';

或使用真实路径

window.location.href='http://www.yourdomain.com/welcome.php';

你可以使用窗口对象

window.location.href="http://example.com/welcome.php";

最简单的方法是用一个 json 响应来模拟它。 然后您解析 json 以查看您得到了什么样的响应。 这是一个工作示例:

索引.html

<!DOCTYPE html>
<html>
<head>
<script>

function updatepage(str){

document.getElementById("result").innerHTML = str;

}


function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }

xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {

      var jsonParse = JSON.parse(xmlhttp.responseText);

      if(jsonParse.redirect){
        window.location.replace(jsonParse.redirect);
      }
      if(jsonParse.success){
        updatePage(success);
      }
    }
  }

xmlhttp.open("GET","login.php",true);
xmlhttp.send();
}
</script>
</head>
<body>

<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>

</body>
</html>

登录.php

<?php


// if($error)
echo json_encode(array('redirect'=>'welcome.php'));

//else
echo json_encode(array('success'=>'<div>logged in</div>'));

?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM