Handle Ajax request and response structure

I want to send an Ajax request to the controller, I do it on the client side

jQuery.ajax({ url: "public/visits/visit/get-visits", type: "POST", dataType: 'json', data: data, success: function(data){ alert(data) }, error:function(){ alert("fail :("); } }); 

on the server side, I process the request like other requests

 public function getVisitsAction() { if (isset($_POST)) { $mapper = new Visits_Model_VisitsMapper(); $allVisits = $mapper->getAllVisits(); echo json_encode($allVisits); } 

When I call the action, an error message appears, and when I check it by mistake, I find that it returns client-side json data to the get-visit.phtml page.

How can I process the response in a success function from a page that sends a json request and redirects it to the get-visit.phtml page?

+4
source share
7 answers

Zend has Zend_Controller_Action_Helper_Json, which performs the following actions:

 $this->_helper->layout()->disableLayout(); $this->_helper->viewRenderer->setNoRender(true); echo json_encode($allVisits); exit; 

So it could be even simpler:

 public function getVisitsActions() { if ($this->getRequest()->isXmlHttpRequest()) { if ($this->getRequest()->isPost()) { $mapper = new Visits_Model_VisitsMapper(); $this->_helper->json($mapper->getAllVisits()); } } else { echo 'Not Ajax'; // ... Do normal controller logic here (To catch non ajax calls to the script) } } 
+17
source

For a more proper way to do this. I would use the following in your controller

 public function getVisitsActions() { if ($this->getRequest()->isXmlHttpRequest()) { if ($this->getRequest()-isPost()) { $mapper = new Visits_Model_VisitsMapper(); $allVisits = $mapper->getAllVisits(); $this->_helper->layout()->disableLayout(); $this->_helper->viewRenderer->setNoRender(true); echo json_encode($allVisits); exit; } } else { // ... Do normal controller logic here (To catch non ajax calls to the script) } } 
+5
source

// client side

 jQuery.ajax({ url: "public/visits/visit/get-visits", type: "POST", dataType: 'json', data: data, success: function(data){ for(i=0;i<data.length;i++){ alert(data[i]); } }, error:function(){ alert("fail :("); } }); 

// server side

 public function getVisitsAction() { $this->_helper->layout()->disableLayout(); $this->_helper->viewRenderer->setNoRender(true); if (isset($_POST)) { $mapper = new Visits_Model_VisitsMapper(); $allVisits = $mapper->getAllVisits(); echo json_encode($allVisits); exit; } 
+2
source

In Zend, when using Zend json, you do not need to further analyze the data in the ajax part. Zend does it on his own. Further in the response header: Content-Type:application/json

server side:

 $this->_helper->json($data); 

client side:

 jQuery.ajax({ url: "public/path to", type: "POST", dataType: 'json', data: data, success: function(data){ var username = data.user_name; ... }, 
+1
source

You will probably have to disable view rendering for your action if called using the HTTP POST method. Here is how I do it:

 Zend_Controller_Front::getInstance()->setParam('noViewRenderer', true); 

There are other ways to do this. You can see more information in the official ViewRenderer documentation .

Hope this helps.

0
source

You can use JsonModel - just return:

return new JsonModel ();

Do not forget to add

use Zend \ View \ Model \ JsonModel;

0
source
 jQuery.ajax({ url: "public/path to", type: "POST", dataType: 'json', data: data, success: function(data){ for(i=0;i<data.length;i++){ alert(data[i]); } }, error:function(){ alert("fail :(""); } }); 
-one
source

Source: https://habr.com/ru/post/1399988/


All Articles