2014-11-24 98 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'); 
    } 
}