2013-02-13 76 views
0

我正在嘗試一個小的ajax應用程序,我只想從我的控制器操作返回一個hello world字符串。 它返回的Hello世界,但與此相伴,還回我的模板文件.. 我想在我的controlelr如何在Zend Framework 2中使用ajax?

$this->_helper->layout()->disableLayout(); 
$this->_helper->viewRenderer->setNoRender(true); 

的操作使用下面的代碼來禁用它的模板,但這返回我這個錯誤

SCREAM: Error suppression ignored for 
(!) Notice: Undefined property: Survey\Controller\SurveyController::$_helper in C:\wamp\www\zend\module\Survey\src\Survey\Controller\SurveyController.php on line 55 

SCREAM: Error suppression ignored for 
(!) Fatal error: Call to a member function layout() on a non-object in C:\wamp\www\zend\module\Survey\src\Survey\Controller\SurveyController.php on line 55 
Call Stack 

我該如何解決這個問題?

編輯

我體改的控制器,使得它看起來像在module..module.config模塊申請目錄此

public function registerAction() 
{ 
    $result = new JsonModel(array(
     'some_parameter' => 'some value', 
     'success'=>true, 
    )); 

    return($result); 
} 

增加戰略

'strategies' => array(
    'ViewJsonStrategy', 
), 

仍,在阿賈克斯響應我得到模板正在返回

+0

layout()不是函數。它應該是:$ this - > _ helper-> layout-> disableLayout(); – StuR 2013-02-13 10:19:15

+0

另外,你是否引導你的佈局助手? – StuR 2013-02-13 10:21:23

+0

@sTuR沒有改進仍然是相同的錯誤 – 2013-02-13 10:23:42

回答

-2

這個工作對我來說:

public function ajaxAction(){ 
    $data = array(
     'var1' => 'var1Value', 
     'var2' => 'var2Value', 
    ); 

    $response = $this->getResponse(); 
    $response->setStatusCode(200); 
    $response->setContent(json_encode($data)); 

    $headers = $response->getHeaders(); 
    $headers->addHeaderLine('Content-Type', 'application/json'); 

    return $response; 
} 

輸出:

{"var1":"var1Value","var2":"var2Value"} 
+1

這不是在zf2中做到這一點的方法。應該使用jsonModel和json渲染器策略。 – Xerkus 2013-02-14 06:33:51

0

我在我的控制器使用:

$view = new ViewModel(array('form'=>$my_form)); 
    //disable layout if request by ajax 
    $view->setTerminal($request->isXmlHttpRequest()); 
    $view->setTemplate('path/to/phtml'); 
    return $view; 
0

用戶想知道如何得到公正的HTML回來,不是JSON作爲安卓回覆報價。

我也希望返回的HTML,所以我可以用它與jQuery的qtip插件,這就是我做到了這一點。 在JavaScript失敗的情況下,我還必須使頁面優雅地退化,例如頁面輸出應該在佈局模板中正確呈現。

/** 
* Tourist Summary action 
* 
* @return ViewModel 
*/ 
public function touristSummaryAction() 
{ 
    // Get the Id 
    $id = $this->params()->fromRoute('id', ''); 

    // Get the data from somewhere 
    $data = array() ; 

    // Get the html from the phtml 
    $view = new ViewModel(
     array(
      'id' => $id , 
      'data' => $data , 
     ) 
    ); 

    //disable layout if request by ajax 
    $view->setTerminal($this->getRequest()->isXmlHttpRequest()); 
    return $view; 

} 
0

看看這個模塊。 www.wasabilib.org 似乎你管理阿賈克斯非常好。

如果你沒有應用程序,你可以使用Wasabilib骷髏https://github.com/WasabiLib/wasabilib_zf2_skeleton_application。它在適當的地方配備了所有必要的資產。

如果你已經有了,你應該克隆模塊的應用程序:https://github.com/WasabiLib/wasabilib

最低要求:jQuery的,ZF2

  1. 添加模塊application.config.php。
  2. 包含wasabilib.min。JS在你layout.phtml頭的jQuery後

它是如何工作 在一個.phtml文件,你有這樣的形式:

<form id="simpleForm" class="ajax_element" action="simpleFormExample" method="POST"> 
<input type="text" name="written_text"> 
<input type="submit" value="try it"> 
</form> 

在別的地方在你的PHTML你可以放置一個顯示響應的元素。

在控制器下面的方法:

public function simpleFormExampleAction(){ 
    $postArray = $this->getRequest()->getPost(); 
    $input = $postArray['written_text']; 
    $response = new Response(new InnerHtml("#element_simple_form","Server  Response: ".$input)); 
    return $this->getResponse()->setContent($response); 
} 

窗體有一類「ajax_element」這說的是,請求將與XMLHTTP請求來完成庫。 如果你沒有給請求元素提供一個id,它不會工作。所以表單的ID是「simpleForm」。這個動作就像普通的請求一樣是「路徑/到/控制器」。

在控制器操作中,實例化新的WasabiLib \ Ajax \ Response對象。 InnerHtml類用於替換,前置和附加HTML或普通文本到選擇器。 在這種情況下,選擇器是一個ID「element_simple_form」。 InnerHtml類的第一個參數是選擇器。 確保您編寫#yourElementId或.yourClassSelector。對於ID爲「#」和類選擇器「。」

第二個參數是要填寫此元素的文本。

響應對象可以處理更多的迴應,你可以用

$response->add($anotherResponseType); 

添加可能的響應類型的列表是在這裏:http://www.wasabilib.org/application/pages/components

該模塊是建立處理Ajax請求的響應一個非常簡單的方法。一旦你瞭解了這個行爲,你就可以處理幾乎所有實際的Ajax需求。

+0

你能解釋一下這將如何工作嗎? – Robert 2015-06-16 08:39:45

+0

請從這裏的鏈接添加重要的點,然後給出鏈接的參考。 – 2015-06-16 08:56:24