php SyntaxError: JSON.parse: JSON 数据第 1 行第 2 列的意外字符 -- FireBug 报告此错误。有什么解决办法吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29741378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:33:00  来源:igfitidea点击:

SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data -- FireBug reports this error. Any solution?

phpjqueryjson

提问by Faizuddin Mohammed

I have used Laravel Response::json to generate a JSON response.

我使用 Laravel Response::json 来生成 JSON 响应。

return Response::json(array('subjects' => $subjects, 'year' => $year, 'sem' => $sem));

When I run the request, I get a valid JSON (tested in JSONLint) as a response.

当我运行请求时,我得到一个有效的 JSON(在 JSONLint 中测试)作为响应。

But the following jQuery method fails: $.parseJSON(data)

但以下 jQuery 方法失败: $.parseJSON(data)

I get the following error in FireBug:

我在 FireBug 中收到以下错误:

SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data

SyntaxError: JSON.parse: JSON 数据第 1 行第 2 列的意外字符

The Response I recieve:

我收到的回复:

{
    "subjects": [
        {
            "id": 1,
            "name": "Control Systems",
            "semester": 1,
            "year": 3,
            "branch_id": 4
        },
        {
            "id": 2,
            "name": "Analog Communications",
            "semester": 1,
            "year": 3,
            "branch_id": 4
        },
        {
            "id": 3,
            "name": "Linear IC Applications",
            "semester": 1,
            "year": 3,
            "branch_id": 4
        },
        {
            "id": 4,
            "name": "Antennas & Wave Propagation",
            "semester": 1,
            "year": 3,
            "branch_id": 4
        }
    ],
    "year": 3,
    "sem": 2
}

And the code where I'm trying to parse it:

以及我试图解析它的代码:

$(document).ready(function() {
    $('#branchAndSubjects').click(function() {
        $.post('/findBranchAndSubjects', {
            roll: roll,
            _token: "{{csrf_token()}}"
        }, function(data) {
            var subjects = $.parseJSON(data);
        });
    });
});

回答by T.J. Crowder

If you're doing the $.parseJSON(data)in an ajax success handlerSince you're doing the $.parseJSON(data)in an ajax success handler, the problem is almost certainly that jQuery has alreadyparsed it for you. jQuery will look at the Content-Typeof the response and, if it's application/json, it will parse it, and provide the parsed result to your success handler. The first thing that will happen if you pass that into $.parseJSONwill be that it will get converted back to a string ("[object Object]", in your case), which $.parseJSONwill then fail to parse.

如果您$.parseJSON(data)在 ajax 成功处理程序中执行既然您$.parseJSON(data)在 ajax 成功处理程序中执行了 ,那么问题几乎肯定是 jQuery已经为您解析了它。jQuery 将查看Content-Type响应的 ,如果是application/json,它将解析它,并将解析的结果提供给您的成功处理程序。如果您$.parseJSON将其传递给它,首先会发生的事情是它将被转换回字符串("[object Object]"在您的情况下为 ),然后$.parseJSON将无法解析。

Just use dataas-is, it's already an object, thanks to the automatic parsing:

只需data按原样使用,由于自动解析,它已经是一个对象:

$(document).ready(function() {
    $('#branchAndSubjects').click(function() {
        $.post('/findBranchAndSubjects', {
            roll: roll,
            _token: "{{csrf_token()}}"
        }, function(data) {
            console.log(data.year);             // 3
            console.log(data.subjects.length);  // 4
            console.log(data.subjects[0].name); // Control Systems
        });
    });
});