3

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.

1 Answer 1

7

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(){

          }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Can you explain how to get the variables from the ajax call in the Zend Controller? I need to interact in PHP with the data sent from my ajax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.