我遇到了这个错误,告诉我有一个意外的标记 C 指向我的 Jquery 文件。经过大量研究,我假设我收到此错误的原因是因为传回的 Json 值已经被解码,因此再次解码将导致此错误。
这个说法属实吗?或者背后还有其他原因吗? 这是我的 json 数据的样子 [{"comments":"Greta"},{"comments":"John"}]
<a onclick="showUser('.$row['ID'].')" >Show Comments</a>
<script>
function showUser(str) {
if (str=="") {
document.getElementById("txtHint").innerHTML="";
return;
}
$.ajax({
type:'post',
url: 'viewCommentsJson.php',
data:{q:str},
success:function(data)
{
data = $.parseJSON(data);
var response;
$.each(data, function(index, value){
response += value+'<br />';
});
$('#txtHint').html(response);
}
});
}
</script>
最佳答案
原因是,您正在尝试解析已经是 json 格式的响应。
$.parseJSON
方法应适用于 string
类型。由于您的服务器响应是 json
,因此您不必再次解析它。
像这样更改你的代码,
$.ajax({
type: 'post',
url: 'viewCommentsJson.php',
data: {
q: str
},
success: function (data) {
var response = "";
$.each(data, function (index, value) {
response += value.comments + '<br />';
});
$('#txtHint').html(response);
}
});
关于jquery - 未捕获的语法错误 : Unexpected token C - JQUERY - Passing and Decoding Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23760652/