I have been trying to understand the follwing program. On server side the following PHP code has written that does the JSON encoding:
<?php
require_once "json/JSON.php";
$json = new Services_JSON();
//convert php object to json
$value = array('first' => 'Steven', 'last' => 'Spielberg', 'address' => '1234 Unlisted Drive');
$output = $json->encode($value);
print($output);
?>
And on client side the JavaScript to implement the AJAX:
<html>
<head>
<script src="ajax.js"></script>
<script>
/**Ajax Request (Submits the form below through AJAX
* and then calls the ajax_response function)
*/
function ajax_request() {
var submitTo = 'ajax_request.php';
//location.href = submitTo; //uncomment if you need for debugging
http('POST', submitTo, ajax_response, document.form1);
}
/**Ajax Response (Called when ajax data has been retrieved)
*
* @param object data Javascript (JSON) data object received
* through ajax call
*/
function ajax_response(data) {
for(var key in data) {
document.form1[key].value = data[key];
}
}
</script>
</head>
<body>
<input type="button" onclick="ajax_request()" value="Do AJAX"><br><br>
<form name="form1">
First <input type="text" name="first"><br>
Last <input type="text" name="last"><br>
Address <input type="text" name="address"><br>
</form>
</body>
</html>
Now my question is that, why in the client side there is no JSON.parse() function used to retrieve JavaScript variable from JSON string?
Perhaps you could find what you search here
http://www.json.org/js.html