Jquery 未捕获的SyntaxError:意外的标识符和求值选项
我有一个功能不能在所有托管环境下正常工作。返回的数据是JSON格式的。我认为问题在于eval()。我可以为eval()使用任何替代方法吗。请帮助我调试这个或建议任何替代方法 三天来,我一直在为这个错误绞尽脑汁(它只在少数人质身上出现,但在其他人质身上效果很好) inLoad.php中的数据如下所示Jquery 未捕获的SyntaxError:意外的标识符和求值选项,jquery,json,eval,web-hosting,Jquery,Json,Eval,Web Hosting,我有一个功能不能在所有托管环境下正常工作。返回的数据是JSON格式的。我认为问题在于eval()。我可以为eval()使用任何替代方法吗。请帮助我调试这个或建议任何替代方法 三天来,我一直在为这个错误绞尽脑汁(它只在少数人质身上出现,但在其他人质身上效果很好) inLoad.php中的数据如下所示 while($row=mysqli_fetch_array($sql)){ $text=$row['mesg']; $fro=$row['fro'];
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);
数据返回为
[{"msg":"hi<br>","last_msg_id_db":"173"}]
main.php:96
main.php:96
[{"msg":"heloo<br>","last_msg_id_db":"174"}]
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
请不要使用eval。只需将
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 + ")");
}
});
“eval”函数作为“数据”接收什么?请提供此变量的值。@izogfif我已更新了我的问题,并添加了要评估的数据。请查看“要评估的数据”-需要数据,而不是生成此数据的代码。在“var json=eval(data);”行中,您会收到哪些符号作为变量“data”的值?@izogfif这是数据。我还在我的问题[{“msg”:“hi
,“last_msg_id_db”:“173”}]main.php:96 main.php:96[{“msg”:“heloo
,“last_msg_id_db”:“174}]中更新了它。“main.php:96”代表什么?你为什么不把它包在别的东西里?像{“main.php”:96}?谢谢你,但是现在错误被警告为“error:parseError(语法错误:意外标记)。我已经更新了我的问题,还添加了要评估的数据。plz review
$.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 + ")");
}
});