[英]Ajax error with the same data (parsererror: SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data)
I've got two pages that use the same js-file to call certain PHP-file and get data from there in JSON format. 我有两个页面使用相同的js文件来调用某些PHP文件,并以JSON格式从那里获取数据。 Although the data that gets in the PHP-file AND data that gets out is exactly the same, Ajax on the second page returns 'parsererror' SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data.
尽管从PHP文件中获取的数据与从中获取的数据完全相同,但第二页上的Ajax在JSON数据的第1行第1列返回'parsererror'SyntaxError:JSON.parse:意外字符。
$.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);
}
}
So have anybody encountered the same problem? 那么有人遇到过同样的问题吗?
This sounds reminiscent of the dreaded BOM . 这听起来让人想起了可怕的BOM 。 Excerpt from that link:
该链接的节选:
At the beginning of a page that uses a Unicode character encoding you may find some bytes that represent the Unicode code point U+FEFF BYTE ORDER MARK (abbreviated as BOM).
在使用Unicode字符编码的页面的开头,您可能会找到一些表示Unicode代码点U + FEFF BYTE ORDER MARK(缩写为BOM)的字节。
The BOM, when correctly used, is invisible.
BOM正确使用后将不可见。
Perhaps check that the file's encoding is set to UTF8 Without BOM . 也许检查文件的编码是否设置为UTF8 Without BOM 。
Maybe a mime type error. 也许是mime类型错误。
Try to add the beforeSend property like this in your AJAX call : 尝试在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");
}
},
...
}
It appears the trouble was in jQuery version. 看来问题出在jQuery版本中。 Now that it's updated all seems to work fine.
现在,它已更新,一切似乎工作正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.