我遇到错误 Uncaught SyntaxError: Unexpected token ILLEGAL

发布于 2025-01-01 05:03:48 字数 2842 浏览 0 评论 0原文

我用以下方式解析 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 + '&nbsp' + 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千仐 2025-01-08 05:03:48

不要手动拼凑 JSON!您在某处犯了一些错误并生成了无效的 JSON。让 PHP 使用 json_encode 处理细节:

$data = array('Comment' => array());
while (...) {
    $data['Comment'][] = array(...);
}

echo json_encode($data);

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:

$data = array('Comment' => array());
while (...) {
    $data['Comment'][] = array(...);
}

echo json_encode($data);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文