繁体   English   中英

具有相同数据的Ajax错误(parsererror:SyntaxError:JSON.parse:JSON数据的第1行第1列出现意外字符)

[英]Ajax error with the same data (parsererror: SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data)

我有两个页面使用相同的js文件来调用某些PHP文件,并以JSON格式从那里获取数据。 尽管从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);
            }
        }

那么有人遇到过同样的问题吗?

这听起来让人想起了可怕的BOM 该链接的节选:

在使用Unicode字符编码的页面的开头,您可能会找到一些表示Unicode代码点U + FEFF BYTE ORDER MARK(缩写为BOM)的字节。

BOM正确使用后将不可见。

也许检查文件的编码是否设置为UTF8 Without 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版本中。 现在,它已更新,一切似乎工作正常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM