这段代码工作正常我关心的是它的输出
Javascript方
function AjaxRetrieve()
{
var rid = document.getElementById('trg').value;
$.get('includes/getChat.php?chat='+uid + '&rid=' + rid + '&name=' + user,function(data)
{
$("#clog").html(data);
});
}
PHP方
$sql6="SELECT msgid FROM thread WHERE combination1=:msgids OR combination2=:submsgids LIMIT 1";
$msg_id = $con4->prepare($sql6);
$msg_id->bindParam(':msgids', $comb, PDO::PARAM_STR);
$msg_id->bindParam(':submsgids', $comb, PDO::PARAM_STR);
$msg_id->execute();
$msgd = $msg_id->fetchColumn();
$tbpre = $msgd;
$sql7 = "SELECT * FROM ".$tbpre."chat_conversation WHERE msgid=:chat";
$stmt7=$con3->prepare($sql7);
$stmt7->bindValue( 'chat', $msgd, PDO::PARAM_STR);
$stmt7->execute();
$rows = $stmt7->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($rows);
输出如下:
[{"msgid":"1","message_content":"asd","username":"ab","message_time":"2014-04-02 13:58:03","recipient":"cd"}]
如何显示与此类似的JSON结果:
1
asd
ab
2014-04-02
cs
答案 0 :(得分:3)
您可以在jQuery中使用json
dataType,然后使用JSON.stringify
使用缩进对对象进行字符串化,并将其输出到pre
标记以保留空格和换行符
function AjaxRetrieve(){
var rid = document.getElementById('trg').value,
data = {chat : uid, rid : rid, name : user};
$.get('includes/getChat.php', data, function(result) {
var pre = $('<pre />', {text : JSON.stringify(result, undefined, 4)})
$("#clog").html(pre);
}, 'json');
}
或只输出值列表
function AjaxRetrieve() {
var rid = document.getElementById('trg').value,
data = {chat: uid, rid: rid, name: user};
$.get('includes/getChat.php', data, function (result) {
var res = $([]);
$.each(result[0], function(key, value) {
res = res.add($('<div />', {text : value}));
});
$("#clog").html(res);
}, 'json');
}
答案 1 :(得分:3)
在客户端解析响应并显示它。
$.get('includes/getChat.php?chat='+uid + '&rid=' + rid + '&name=' + user,function(data){
var obj = JSON.parse(data);
var temp = obj[0].msgid + "<br>"+ obj[0].message_content + "<br>"+ obj[0].username + "<br>"+ obj[0].message_time + "<br>"+ obj[0].recipient;
$("#clog").html(temp);
});
答案 2 :(得分:2)
PHP 5.4提供了JSON_PRETTY_PRINT
选项,可用于json_encode()
调用。
以下是链接:
http://php.net/manual/en/function.json-encode.php
<?php
$json_string = json_encode($data, JSON_PRETTY_PRINT);