<?php echo link_to_remote('Refresh the letter', array(
'url' => 'publishing/refresh',
'complete' => 'updateJSON(ajax)'
)) ?>
<?php echo javascript_tag("
function updateJSON(ajax)
{
json = ajax.responseJSON;
for (var i = 0; i < json.length; i++)
{
Element.update(json[i][0], json[i][1]);
}
}
") ?>
$this->getResponse()->setHttpHeader('Content-Type','application/json; charset=utf-8');e "voalá"!!
$output = '[["title", "My basic letter"], ["name", "Mr Brown"]]';
return $this->renderText('('.$output.')');
Sometimes the application is designed to send JavaScript code as a response. If
the content type of the response matches the MIME type of JavaScript then this
is true and Prototype will automatically eval() returned code. You don't need
to handle the response explicitly if you don't need to.
Alternatively, if the response holds a X-JSON header, its content will be parsed, saved as an object and sent to the callbacks as the second argument:
new Ajax.Request('/some_url', { method:'get',
onSuccess: function(transport, json){
alert(json ? Object.inspect(json) : "no JSON object");
}
});
Use this functionality when you want to fetch non-trivial data with Ajax but want to avoid the overhead of parsing XML responses. JSON is much faster (and lighter) than XML.