Hi everyone.
I love JSON and using Prototype has help me reduce the code to handle
AJAX responses.
But one thing keeps me confused and sometimes even frustrated.
I don't really understand what is going on with the headers, so if
someone could take some time to explain what is going on, I (and
probably a lot more people) would be very thankful.
The thing is this. On my local server (Mac OS Leopard Server 10.5.4,
PHP 5.2.5 by
entropy.ch, Apache 2.2 Apple's default) after trying
different approaches, I finally got JSON to work using this:
<?php
$json = json_encode($status);
header('X-JSON:('.$json.')');
?>
and my javascript to handle the JSON response:
onSuccess: function(transport,json) {
Beautiful, works great, on my Mac at least
When I upload this to the server, it doesn't work.
I don't know what kind of machine it is, but it is running PHP 5.2.0
and Apache 2.2 and probably Linux.
After a few hours of searching on the net and trial/error, this
worked:
<?php
$json = json_encode($status);
header('Content-type: application/json');
echo $json;
?>
And the javascript:
onSuccess: function(transport) {
var json = transport.responseText.evalJSON();
So, anyone knows WHY? I would really like to understand what is going
on here.
Thank you.
Joe