我遇到错误 Uncaught SyntaxError: Unexpected token ILLEGAL
我用以下方式解析 JSON
eval() 函数 `
我有一个错误
未捕获的语法错误:意外的标记非法
这是我的 javascript 代码:
var commentPerpost = document.getElementById('commentPerpost');
var response = eval('('+receiveReq.responseText+')');
for(var i=0;i < response.Comment.comment.length; i++) {
commentPerpost.innerHTML += '<li><div id="Cclose" align="right">' +
'<input id="Userid" type="hidden" name="Userid" value="' + response.Comment.comment[i].iduser + '" /></div>' +
'<div id="Cprofil"><img src="../91mbr27pg09/foto_profil/' + response.Comment.comment[i].ava +
'" style="width: 40px; height: 40px;" alt="" />' +
'<h4 style="margin: 0; padding-top: 5px;">' + response.Comment.comment[i].username + '</h4></div>' +
'<div id="Cpost"><div style="max-width: 330px;">' + response.Comment.comment[i].comment +
'</div><div class="Cdatetime">' + response.Comment.comment[i].tgl + ' ' + response.Comment.comment[i].time +
'</div></div><div class="clearBoth BdrBottomN"></div></li>';
}
而我的 php 代码是:
<?php
$json = '{"Comment": {';
if(!isset($_GET['idnews'])) {
} else {
$idnews = db_input($_GET['idnews']);
$last = (isset($_GET['last']) && $_GET['last'] != '') ? $_GET['last'] : 0;
$sql_comment = "SELECT c.id_user, c.id_comment,c.id_judul,c.email,c.tanggal,c.id_member,c.comment,m.id,m.username,m.foto_profil,k.id,k.judul FROM comment c, member m, k_news k WHERE c.id_user = m.id AND c.id_judul = k.id AND k.id = '$idnews' AND c.id_comment > '$last' ORDER BY c.tanggal ASC";
$qry_comment = db_query($sql_comment);
if(db_num_rows($qry_comment) > 0) {
$json .= '"comment":[ ';
while($data_comment=mysql_fetch_array($qry_comment)){
$json .= '{';
$json .= '"idcomment": "' . htmlspecialchars($data_comment['id_comment']) . '",
"iduser": "' . $data_comment['id_user'] . '",
"username": "' . $data_comment['username'] . '",
"comment": "' . htmlspecialchars($data_comment['comment']) . '",
"ava": "' . htmlspecialchars($data_comment['foto_profil']) . '",
"tgl": "' . tgl_indo($data_comment['tanggal']) . '",
"time": "' . time_indo($data_comment['tanggal']) . '"
},';
}
$json .= ']';
}
else {
$json .= '"comment":[]';
}
}
$json .= '}}';
echo $json;
?>
请帮助我如何修复错误?
I parse JSON with
eval() function
`
and I have an error
Uncaught SyntaxError: Unexpected token ILLEGAL
this is my javascript code :
var commentPerpost = document.getElementById('commentPerpost');
var response = eval('('+receiveReq.responseText+')');
for(var i=0;i < response.Comment.comment.length; i++) {
commentPerpost.innerHTML += '<li><div id="Cclose" align="right">' +
'<input id="Userid" type="hidden" name="Userid" value="' + response.Comment.comment[i].iduser + '" /></div>' +
'<div id="Cprofil"><img src="../91mbr27pg09/foto_profil/' + response.Comment.comment[i].ava +
'" style="width: 40px; height: 40px;" alt="" />' +
'<h4 style="margin: 0; padding-top: 5px;">' + response.Comment.comment[i].username + '</h4></div>' +
'<div id="Cpost"><div style="max-width: 330px;">' + response.Comment.comment[i].comment +
'</div><div class="Cdatetime">' + response.Comment.comment[i].tgl + ' ' + response.Comment.comment[i].time +
'</div></div><div class="clearBoth BdrBottomN"></div></li>';
}
and my php code is :
<?php
$json = '{"Comment": {';
if(!isset($_GET['idnews'])) {
} else {
$idnews = db_input($_GET['idnews']);
$last = (isset($_GET['last']) && $_GET['last'] != '') ? $_GET['last'] : 0;
$sql_comment = "SELECT c.id_user, c.id_comment,c.id_judul,c.email,c.tanggal,c.id_member,c.comment,m.id,m.username,m.foto_profil,k.id,k.judul FROM comment c, member m, k_news k WHERE c.id_user = m.id AND c.id_judul = k.id AND k.id = '$idnews' AND c.id_comment > '$last' ORDER BY c.tanggal ASC";
$qry_comment = db_query($sql_comment);
if(db_num_rows($qry_comment) > 0) {
$json .= '"comment":[ ';
while($data_comment=mysql_fetch_array($qry_comment)){
$json .= '{';
$json .= '"idcomment": "' . htmlspecialchars($data_comment['id_comment']) . '",
"iduser": "' . $data_comment['id_user'] . '",
"username": "' . $data_comment['username'] . '",
"comment": "' . htmlspecialchars($data_comment['comment']) . '",
"ava": "' . htmlspecialchars($data_comment['foto_profil']) . '",
"tgl": "' . tgl_indo($data_comment['tanggal']) . '",
"time": "' . time_indo($data_comment['tanggal']) . '"
},';
}
$json .= ']';
}
else {
$json .= '"comment":[]';
}
}
$json .= '}}';
echo $json;
?>
plesee help me how I fix the error ??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要手动拼凑 JSON!您在某处犯了一些错误并生成了无效的 JSON。让 PHP 使用
json_encode
处理细节:Don't manually cobble together JSON! You're making some mistake somewhere and are producing invalid JSON. Let PHP take care of the details using
json_encode
: