javascript - PHP 重定向到 AJAX 未定义

标签 javascript php jquery ajax

我想通过 AJAX 使用 PHP 进行重定向,但总是显示为未定义的 url。我试图让 PHP 检查用户,如果为 true,则通过,但如果为 false,则进行重定向。有人可以帮我吗?

按钮:

<div class="checkout-button"><a id="button-quotation" class="button">Click Me</a></div> 

AJAX:

$(document).ready(function(){
$('#button-quotation').click(function(){
    $.ajax({
      type: 'POST',
      url: 'index.php?route=checkout/cart/quotation',
      success: function(data){
        window.location.href = data.redirect;
      }
    })
});});

PHP:

public function quotation(){
    if (!$this->customer->isLogged()) {
        $this->session->data['redirect'] = $this->url->link('checkout/cart', '', 'SSL');

        $this->redirect($this->url->link('account/login', '', 'SSL'));
    }
}

最佳答案

您正在框架中使用框架 $this->redirect 用于重定向(即 header("location:xx.php")。但是这里您需要打印它,然后只有 ajax 才能获取 url...use echo 重定向

echo $this->url->link('account/login', '', 'SSL');

并且在 ajax get rquest 中也只使用 data 而不是 data.redirect。如果您使用任何 json 格式,那么只会使用它

success: function(data){
    window.location.href = data;
  }

关于javascript - PHP 重定向到 AJAX 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27104457/

相关文章:

javascript - 原生 Jquery 选择器 VS 缓存 object.find()

javascript - 索引 : Select and Insert 的 IndexedDB 性能

javascript - 为什么我不能更新我在 jQuery 中迭代的数据属性?

javascript - 数据表在更改事件时重新加载

JavaScript MDN 教程 - 有关浮点不精确性的示例

javascript - 循环 JS 函数直到满足 if or else 条件

php - 如果 Count 为零,如何在 Laravel 中获取记录

php - Symfony 中的无效表单,没有错误

php - 为什么我的 session 仍然存在?

jquery - 如何使用jquery将dom对象构建到html中