2014-11-24 99 views
1

我想通过AJAX与PHP进行重定向,但始终显示为未定义的url。我试图让PHP检查用户,如果它真的通过,但如果错误,它会重定向。任何人都可以帮助我吗?PHP重定向到AJAX undefined

按钮:

<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')); 
    } 
} 
+0

如果你只是做'回声会发生什么$ this-> url-> link('account/login','','SSL')'在你的PHP文件中? – 2014-11-24 12:05:53

+0

您使用的是PHP框架吗? – 2014-11-24 12:06:11

+0

Roy M J同样的错误。 Davide Pastore OpenCart。 – Corlleone 2014-11-24 12:10:07

回答

1
您使用的框架

工程框架$这个 - >重定向是用来重定向(即头(“location:xx.php”)。但是在这里你需要打印它,然后只有ajax会得到url ...使用回声insted的重定向

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

和阿贾克斯获得rquest也只是用数据未data.redirect.if你使用任何JSON格式则仅将它用于

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

非常感谢sasi kanth。这解决了我的问题。 – Corlleone 2014-11-24 12:20:05

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

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

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