简体   繁体   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)

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.

相关问题 由于存在json,Ajax表单无法正常工作(抛出parsererror:SyntaxError:JSON.parse:JSON数据第1行第1列的数据意外结束) - Ajax form not working because of json (parsererror thrown: SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data) SyntaxError:JSON.parse:JSON数据的第1行第1列出现意外字符吗? - SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data? 语法错误:JSON.parse:JSON 数据的第 1 行第 1 列出现意外字符 - SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data 语法错误json.parse json数据第1行第1列的意外字符 - syntaxerror json.parse unexpected character at line 1 column 1 of the json data SyntaxError:JSON.parse:JSON数据第3行第1列的意外字符 - SyntaxError: JSON.parse: unexpected character at line 3 column 1 of the JSON data SyntaxError:JSON.parse:JSON数据的第1行第2列出现意外字符 - SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data jQuery ajax语法错误json.parse json数据的第1行第1行出现意外字符 - Jquery ajax syntaxerror json.parse unexpected character at line 1 column 1 of the json data JSON错误:SyntaxError:JSON.parse:JSON数据的第2行第1列出现意外字符 - JSON error : SyntaxError: JSON.parse: unexpected character at line 2 column 1 of the JSON data 来自寄存器的错误:TypeError:res 未定义/ SyntaxError:JSON.parse:JSON 数据的第 1 行第 1 列出现意外字符 - error from register: TypeError: res is undefined/ SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data 我不断收到此错误SyntaxError:JSON.parse:JSON数据第1行第2列的意外字符 - I keep getting this error SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM