2013-02-13 74 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