我一直在努力了解以下方案。在服务器端,编写了以下PHP代码来执行JSON编码:
<?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);
?>
在客户端使用JavaScript来实现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>
现在我的问题是,为什么在客户端没有用于从JSON字符串中检索JavaScript变量的JSON.parse()函数?