0

私は次のプログラムを理解しようとしています。サーバー側では、JSON エンコーディングを行う次の PHP コードが記述されています。

<?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 に答える