我想通过 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/