0

我一直在尝试理解以下程序。在服务器端,以下 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() 函数?

4

1 回答 1

0

也许你可以在这里找到你搜索的内容 http://www.json.org/js.html

于 2012-08-21T13:11:01.620 回答