Getting an Ajax response from Zend Framework Controller

ajax, jquery, response, zend-framework

Solution

Your url is `"localhost/some-activity"` change that to `"localhost/someactivity"` in your action

public function someactivityAction(){

//do stuff

echo "hellopanda";

exit;

}

if you want to return an array make sure to encode like

echo $this->_helper->json($yourarray);exit;

and your ajax will be like this

$.ajax({  

      type: "POST",  
      url: "localhost/someactivity",
      data: dataString, 
      dataType: 'json', //if you are returning array
      success: function(data) { 
           for(int i=0;i<data.length;i++){
                 alert(data[i]['yourindexofarray']);
              }
      },
      error: function(data){
               alert( "Data is: " + data);
        //do something with data 
          },
      onComplete: function(){

          }
});

Problem

I'm doing an Ajax request on one of my views to a Controller but I am unable to send back a response to the Ajax method. In the snippet below, I am trying to send the word 'hellopanda' back but in the alert message, I'll get data as an object. View : ``` $.ajax({ type: "POST", url: "localhost/some-activity", data: dataString, success: function(data) { alert( "Data is: " + data); //do something with data }, error: function(data){ alert( "Data is: " + data); //do something with data }, onComplete: function(){ } }); ``` Controller: ``` public function someActivityAction(){ //do stuff echo "hellopanda"; } ``` I'm pretty sure the echo is the problem. Any insights on to how to do a proper response to the view would be greatly appreciated.

Original source