2012-12-03 33 views
0

我有一個在所有主機環境中都無法正常工作的函數。返回的數據是JSON格式。我認爲問題是與eval()。我可以使用eval()的任何替代方法嗎?請幫我調試這個或建議任何替代方法。未捕獲SyntaxError:意外標識符和eval備選

三天,我scrathing我的頭,出現此錯誤(只出現在少數hostings但對他人正常工作)

數據inLoad.php是

while($row=mysqli_fetch_array($sql)){ 
     $text=$row['mesg']; 
     $fro=$row['fro']; 
     $tocomr=$row['tocom']; 
     $last_msg_id_db=$row_m['maxid']; 


     $response=array(); 

     //$response['msg']=$table; 
     $response['msg']=$text; 
     $response['last_msg_id_db']=$last_msg_id_db; 
     $final_response[]=$response; 

    } 
      echo json_encode($final_response); 

AND數據返回爲

[{"msg":"hi<br>","last_msg_id_db":"173"}] 
    main.php:96 

    main.php:96 
    [{"msg":"heloo<br>","last_msg_id_db":"174"}] 

jquery函數如下。在Chrome的日誌

Uncaught SyntaxError: Unexpected identifier main.php:95 
$.ajax.success main.php:95 
fire jquery.min.js:974 
self.fireWith jquery.min.js:1082 
done jquery.min.js:7788 
callback 
+0

「eval」函數接收到「data」是什麼?提供這個變量的值。 – izogfif

+0

@izogfif我已更新我的問題,並且還添加了要evaled.plz審查數據 –

+0

「要被撤銷的數據」 - 需要數據,而不是生成此數據的代碼。您在「var json = eval(data);」一行上收到什麼符號作爲變量「data」的值? – izogfif

回答

0

請不要使用eval給出

function chat_com_one(id, name) { 
$('#chatcom').show('fast'); 
(function chatcom_load_one(id, name) { 

    $.ajax({ 
     type:"Post", 
     url:"load.php", 
     data:{ 
      tocom:id, 
      last_msg_id:last_msg_id 
     }, 


     success:function(data) { 

      var json = eval(data); 
      $.each(json, function(i, row) { 
       $("#commidwin").append(row['msg']); 
       last_msg_id = row['last_msg_id_db']; 
      }); 
      setTimeout(chatcom_load_one(id, name), 500); 
     }, 
     error:function(XMLhttprequest, textstatus, errorthrown) { 
      alert("error:" + textstatus + "(" + errorthrown + ")"); 
     } 




    }); 
}(id, name)); 
} 

下面的錯誤。只需將dataType設置爲json,jQuery將爲您解碼JSON。

$.ajax({ 
    type:"POST", 
    url:"load.php", 
    dataType:"json", 
    data:{ 
     tocom:id, 
     last_msg_id:last_msg_id 
    }, 
    success:function(data) { 
     var json = data; 
     $.each(json, function(i, row) { 
      $("#commidwin").append(row['msg']); 
      last_msg_id = row['last_msg_id_db']; 
     }); 
     setTimeout(chatcom_load_one(id, name), 500); 
    }, 
    error:function(XMLhttprequest, textstatus, errorthrown) { 
     alert("error:" + textstatus + "(" + errorthrown + ")"); 
    } 
}); 
+0

謝謝你,但現在錯誤提示爲「error:parseError(Syntax error:unexpected token <)」 –

+0

我已經更新了我的問題,並且還添加了要evaled.plz評論的數據 –

相關問題