我一直在做很多事情,包括在我有一个有趣的问题时,让AJAX请求从我的网站的后端获取信息。在PHP中使用html样式,然后发送给客户端,还是以json方式发送数据,然后在javascript中使用样式更好?PHP中的HTML标记与Javascript
由于这个问题是怎么样的vauge我就举一个例子:
<?php
$data = array();
$data[0] = array("username" => "Supericy", "content" => "just a sample message 1");
$data[1] = array("username" => "Supericy", "content" => "just a sample message 2");
$data[2] = array("username" => "Supericy", "content" => "just a sample message 3");
// etc...
// now this is the choice:
if ($json)
{
return json_encode($data);
}
else
{
// or
$returnString = "";
for (...)
{
$returnString .= '<div class="username">' . $data[i]["username"] . '</div>';
$returnString .= '<div class="content">' . $data[i]["content"] . '</div>';
}
return $returnString;
}
?>
然后在我的javascript:
// get the information as raw json
var data = ajax_request(json = true);
var author = document.createElement('div');
author.className = "author";
author.innerHTML = data[i]["username"];
var content = document.createElement('div');
content.className = "content";
content.innerHTML = data[i]["content"];
body.appendChild(author);
body.appendChild(content);
// -- OR --
// get the information as pre-formated html
var data = ajax_request(json = false);
body.innerHTML += data;
“取决于”是唯一有用的答案。 – Hamish 2012-04-27 01:44:45
如果有人禁用JavaScript,会发生什么情况? – jprofitt 2012-04-27 01:52:20
那么他会怎么做一个ajax电话? – 2012-04-27 01:57:13