How can I echo something at the end of the cakePHP json/xml response?
I need this in order to add JSONP support (Because i need to add the callback at the beginning and the ‘);’ at the end
The controller uses this :
public function json() {
//...code to populate $jsonObjects
$this->set('objetos',$jsonObjects);
$this->set('_serialize', 'objetos');
}
Firstly, I’m assuming you have Routes set up to correctly handle JSON/XML responses
In your routes file:
Secondly, you would need to make sure the call to your example uses the
.jsonextension or the Accept header isapplication/jsonYou then check for the callback in your controller
in your view file (ex: View/Users/json/index.ctp) you should have something like this:
I use something similar but didn’t test the exact example above so you may need to clean it up. Also make sure you clean up the callback var so you aren’t leaving a security hole by outputting exactly what is in the query string parameter.