2012-02-04 68 views
0

我是Zend Framework的初學者。我通過像這樣的AJAX查詢傳遞變量

$.ajax({ 
    method: "GET", 
    url: "/filename/fetch-client-data.php", 
      dataType: 'json', 
    // and so on 
} 

我需要獲取窗體傳遞的變量。我不知道如何使用jquery.ajax中的$ _GET ['varaible name']。

這裏是我想要的控制器功能

public function fetchClientDataAction() 
{ 
    $this->_helper->layout->disableLayout(); 
    $this->_helper->viewRenderer->setNoRender(TRUE); 
    $this->get('variablename')=$variable_name; 
} 

有人點我在正確的方向?

回答

1

您訪問超級全球$ _GET在PHP這樣的: -

public function fetchClientDataAction() 
{ 
    $this->_helper->layout->disableLayout(); 
    $this->_helper->viewRenderer->setNoRender(TRUE); 
    $variable_name = $_GET['variable_name']; 
} 

有這樣做的更多的 'Zend框架' 像這樣: -

$variable_name = $this->getRequest()->getParam('variable_name'); 

顯然,你需要根據您的使用情況清理您收到的數據。

如果您要求通過jQuery將數據發送回客戶端進行處理,那麼您無需擔心$ _GET或$ _POST。您只需將您的數據作爲JSON的響應主體: -

public function fetchClientDataAction() 
{ 
    $this->_helper->layout->disableLayout(); 
    $this->_helper->viewRenderer->setNoRender(TRUE); 
    $variable_name = $_GET['variable_name']; 
    //now we can respond 
    $result = doSomethingWithVariable_name($variable_name); 
    header('OK', true, 200); 
    header('Content-type: application/json'); 
    echo json_encode($result); 
} 
+0

感謝@ JSON的vascowhite..the輸出返回類似 [{「CLIENT_NAME」:「史蒂夫·哈里斯」,「狀態」:」 TX「}] 是不是因爲[]括號不能顯示在窗體中? – Micheal 2012-02-06 00:19:54

+0

str_replace(array('[',']'),'',json_encode($ this-> view-> lead_query)) 上述代碼適用於我 – Micheal 2012-02-06 01:28:16