これは実際にはこの "Why need to use JSON in php and AJAX"へのフォローアップの質問です。と[Db to PHP to JSON to AJAX to HTML]速度差
[S.1]
だから、PHPと低速のデータベースから直接AJAXとHTML内のデータを表示していますか?
Ex: just imagine this with AJAX but w/o JSON
PHP $query ...etc
echo "<p>".$row['name']." ".$row['comment']."</p>";
[S.2]
HTML
..and AJAX
// Js
$.getJSON("comments.php?jsoncallback=?", function(data) {
//loop through all items in the JSON array
for (var x = 0; x < data.length; x++) {
//create a container for each comment
var div = $("<div>").addClass("row").appendTo("#comments");
//add author name and comment to container
$("<label>").text(data[x].name).appendTo(div);
$("<div>").addClass("comment").text(data[x].comment).appendTo(div);
}
});
でそれを表示する前に最初のJSON
でPHPEx: this with JSON and
for ($x = 0, $numrows = mysql_num_rows($query); $x < $numrows; $x++) {
$row = mysql_fetch_assoc($query);
$comments[$x] = array("name" => $row["name"], "comment" => $row["comment"]);
}
$response = $_GET["jsoncallback"] . "(" . json_encode($comments) . ")";
echo $response;
とDBからのデータを渡すよりより速い方法ですか?それとも速度差があるのでしょうか?
Tia、これは価値ある質問だと思いますが、私はまだJSONが初めてです。
実際のスピードの違いは、実際のレンダリングがJSONのクライアント側(または「HTMLの作成」)で行われるということだけです。間違いなく、それはずっと速いです。 – Robus