JSON in prototype 1.6.0: basic example
October 4, 2007That right guys, prototype 1.6.0 is on it's way and there is one aspect I want to share with you. In stead of using the old-fashioned 'http-header' way to transport JSON, the prototype framework now eats files with the content-type 'application/json'. A short example:
- First, we will use prototype to make an ajax request.
new Ajax.Request('ajax.php',{ onSuccess: handleSuccess }, method: 'get' });
- After this, on the serverside we set the json headers and output the formatted data using the Services_JSON class:
<?php require_once "services_json.class.php"; /* Set the JSON header */ header("content-type:application/json"); /* Format some data */ $aData = array("Car", "Plane", "Train"); $oJson = new Services_JSON(SERVICES_JSON_LOOSE_TYPE); /* Output */ echo $oJson->encode($aData); ?>
- And finally this alerts 'Car', 'Plain' and 'Train':
function handleSuccess(transport) { $A(transport.responseJSON).each(function(str) { alert(str); }); }




Alright cool, but what are the advantages? I think i lack some knowledge of the whole JSON thing here.
Comment by Martijn de Kuijper — October 4, 2007 @ 8:14 am
Well yeah, JSON is alright. By the way, PHP5’s got built-in json functions, so we don’t have to use PEAR no more
Thx
Comment by kovshenin — August 14, 2008 @ 12:02 pm