【问题标题】:Force redirect after Jquery AJAX Call from PHP从 PHP 调用 Jquery AJAX 后强制重定向
【发布时间】:2013-08-08 02:29:57
【问题描述】:

我有一个自定义框架,我在我的 Request 文件中检测请求是否是 ajax。在我的基本控制器中,我检查用户是否已登录:

If user is not logged in:
  1. if request is ajax - force JS redirect from PHP (unable to do)
  2. if request is not ajax - PHP redirect (header, works)

我无法让号码 1 工作。

这是我的重定向代码:

//User::Redirect
public function redirect($url = SITE_URL, $isAjax = false) {
    if ($isAjax === true) {
        //its ajax call, echo js redirect
        echo '<script>window.location = ' . $url . ';</script>';
    } else {
        header("Location: {$url}");
    }
    exit;
}

登录检查代码:

//Basecontroller::__construct
if ( ! $this->user->isLoggedIn()) {
   //redirect to login page
   $this->user->redirect('/login', $request->ajax()); 
   exit;
}

但不是在 ajax 调用上重定向,而是输出&lt;script&gt;window.location = URL&lt;/script&gt;

注意:

我知道我可以对我的每个 AJAX 调用添加检查以从那里进行重定向,但我试图避免这种情况并让我的 PHP 脚本在基本控制器中检测并重定向,而不是我对所有 AJAX 调用添加检查(很多)。

【问题讨论】:

  • 作为一个建议,我倾向于通过一个通用函数传递所有的ajax调用,如果已经发送了一个http重定向,这个函数会处理http重定向。
  • 所以你有一个包含所有 ajax 调用的巨大文件?
  • 不,我有一个小文件,其中包含一个通用的 ajax 函数,有几个用于不同目的的变体,但最终每个请求都通过相同的函数。
  • 你的意思是一个Javascript函数,你用它来调用所有的ajax?
  • 是的。所以我所有的 ajax 调用都以某种方式通过这个函数,它处理各种 http 响应,包括重定向。

标签: php jquery http-redirect


【解决方案1】:

您的 Javascript 重定向需要脚本标签。试试这个:

public function redirect($url = SITE_URL, $isAjax = false) {
    if ($isAjax === true) {
        //its ajax call, echo js redirect
        echo '<script>window.location = ' . $url . ';</script>';
    } else {
        header("Location: {$url}");
    }
    exit;
}

就在你的 ajax 中处理这个而言,假设是 jQuery

$.ajax(
    {
        type: "POST",
        url: url,
        data: postParams,
        success: function(data)
        {
            //do something with success response
        },
        error : function(httpObj, txtStatus)
        {
            if(httpObj.status == 401)
            {//redirect to login
                var loginUrl = '/login';
                document.location.href = loginUrl;
            }
            else if(httpObj.status == ...)  //etc
            {//do something

            }
            else
            {
                alert("Ajax\n Error: " + txtStatus + "\n HTTP Status: " + httpObj.status);
            }
        }

【讨论】:

  • 您可能缺少周围的 html。看看上面尼克的回应。
  • 即使使用 html 仍然没有
  • 添加此行header("HTTP/1.1 401 Unauthorized");
【解决方案2】:

客户端应该如何知道这是 javascript?

尝试用脚本标签包裹它 即

<html>
<script>
window.location = 'testpage.html'
</script>
</html>

【讨论】:

    【解决方案3】:

    此代码对我有用。如果 ajax 调用发现用户未登录(例如:用户打开浏览器并且会话已过期),PHP 会将刷新代码作为 JS 或元标记回显到浏览器以强制刷新整个页面。

    $url = '/index.php';
    
    echo '<script type="text/javascript">';
    echo 'window.location.href="'.$url.'";';
    echo '</script>';
    echo '<noscript>';
    echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
    echo '</noscript>';
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-11
      • 2010-09-15
      • 1970-01-01
      • 1970-01-01
      • 2011-02-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多