2012-02-01 77 views
-2

我解析JSON与我有一个错误未捕获的SyntaxError:意外的标记非法

eval() function `

,我有一个错误

Uncaught SyntaxError: Unexpected token ILLEGAL

这是我的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; 
?> 

plesee帮助我如何修复错误?

+0

为什么你删除[老问题](http://stackoverflow.com/questions/9089258/i-have-an-error-uncaught-syntaxerror-意外令牌非法)并重新发布相同的东西? – deceze 2012-02-01 01:37:39

+0

,因为我在我的旧问题中有错误的帖子。 – qdoet 2012-02-01 01:40:24

回答

2

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

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

echo json_encode($data); 
+0

非常感谢你这是有益的:) – qdoet 2012-02-01 05:39:08

相关问题