Reputation: 9858
I want to send Ajax request to controller, I make like this in 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 :(");
}
});
at the server side I handle the request as 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, fail alert occurs and when I check it out through fire bug I found that it returns the json data to client side to page get-visit.phtml.
How can I handle the response in the success function from the page that send the json request and redirecting it to get-visit.phtml page?
Upvotes: 5
Views: 28973
Reputation: 23
You can use JsonModel - simply return:
return new JsonModel();
Don't forget to add
use Zend\View\Model\JsonModel;
Upvotes: 0
Reputation: 459
Zend has Zend_Controller_Action_Helper_Json which do these 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)
}
}
Upvotes: 20
Reputation: 2673
You'll probably need to disable the view rendering for your action in case it's being called with the POST
HTTP method. Here's how I do it:
Zend_Controller_Front::getInstance()->setParam('noViewRenderer', true);
There are other ways to do it, though. You can see more information in the official ViewRenderer documentation.
Hope that helps.
Upvotes: 0
Reputation: 11
In Zend while using Zend json you don't need to further parse the data in ajax portion . Zend does it on its own.
Further in 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;
...
},
Upvotes: 1
Reputation: 1
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 :("");
}
});
Upvotes: -1
Reputation: 41
For a more correct way of doing 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)
}
}
Upvotes: 4
Reputation: 7954
//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;
}
Upvotes: 2