이것은 실제로이 "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에 표시하기 전에
Ex: 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;
JSON
에 PHP와 DB로부터 먼저 데이터를 전달하는보다..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);
}
});
어느 더 빠른 방법인가요? 아니면 속도 차이가 있습니까?
티아 (Tia), 이것이 가치있는 질문이 되길 바랍니다. 아직 JSON을 처음 사용합니다.
실제로 유일한 차이점은 JSON의 클라이언트 측 (또는 HTML 작성)에서 실제 렌더링이 발생한다는 것입니다. 의심 할 여지가 있지만 훨씬 빠릅니다. – Robus