Javascript 相同数据的Ajax错误(parsererror:SyntaxError:JSON.parse:JSON数据第1行第1列的意外字符)
我有两个页面使用相同的js文件调用特定的PHP文件,并从中获取JSON格式的数据。尽管PHP文件中的数据和输出的数据完全相同,但第二页的Ajax在JSON数据的第1行第1列返回'parsererror'SyntaxError:JSON.parse:unexpected字符Javascript 相同数据的Ajax错误(parsererror:SyntaxError:JSON.parse:JSON数据第1行第1列的意外字符),javascript,php,ajax,json,Javascript,Php,Ajax,Json,我有两个页面使用相同的js文件调用特定的PHP文件,并从中获取JSON格式的数据。尽管PHP文件中的数据和输出的数据完全相同,但第二页的Ajax在JSON数据的第1行第1列返回'parsererror'SyntaxError:JSON.parse:unexpected字符 $.ajax({ type: 'POST', dataType: "json", data: {objtyp: this.objtyp, objid: this.
$.ajax({
type: 'POST',
dataType: "json",
data: {objtyp: this.objtyp, objid: this.objid},
url: '/admin/getfieldsadd.php',
success: function(data) {
//not going to happen
},
error: function (xhr, status, text) {
switch (status) {
case 404:
alert('File not found');
break;
case 500:
alert('Server error');
break;
case 0:
alert('Request aborted');
break;
default:
alert('Unknown error: ' + status + " " + text);
}
}
有人遇到过同样的问题吗?这听起来让人想起了过去。摘自该链接: 在使用Unicode字符编码的页面开头,您可以 可能会找到一些表示Unicode代码点U+FEFF字节的字节 订单标记(缩写为BOM) 正确使用时,BOM表不可见
也许可以检查文件的编码是否设置为UTF8而没有BOM表 可能是mime类型错误 尝试在AJAX调用中添加beforeSend属性,如下所示:
$.ajax({
type: 'POST',
dataType: "json",
data: {objtyp: this.objtyp, objid: this.objid},
url: '/admin/getfieldsadd.php',
beforeSend: function(x) {
if(x && x.overrideMimeType) {
x.overrideMimeType("application/json");
}
},
...
}
问题似乎出在jQuery版本中。现在它已经更新了,似乎一切正常。虽然这是可能的,但更常见的原因是在调用
echo json\u encode()
@Barmar之前在脚本中打印一些内容,这是什么意思?PHP或JS文件?请查看开发人员工具中的响应。请确保响应中除了JSON之外没有其他内容。@Barmar,@void,前面的评论。这没有帮助,唉。