Javascript 为什么 JSON.parse 不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5300169/
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-23 16:33:28  来源:igfitidea点击:

Why JSON.parse is not working?

javascriptjson

提问by Frias

I set the dataType to 'text' because I don't want to Jquery parse my JSON automatically. My code is the following:

我将 dataType 设置为“text”,因为我不想让 Jquery 自动解析我的 JSON。我的代码如下:

var membId = '5';
$('#submitNewDescription').live('click',function(){
    //An ajax request is made to update the DB
    $.ajax({
        url: '../../cgi-bin/qualification.py',
        type: 'POST',
        data: ({newDescription:$('#newDescription').val(),id:membId}),
        dataType: 'text',
        cache: 'false',
        success: function(data){
            json = JSON.parse(data);
            console.log(data);
            console.log(json);
        }
    });
});

And it returns that string: {"error":["ORA-01031 insufficient privileges"]} in both console.log commands. It means that the parse isn't working since it doesn't return a JavaScript object. JSONLint says to me that is a valid JSON.

它在两个 console.log 命令中都返回该字符串:{"error":["ORA-01031 enough privileges"]}。这意味着解析不起作用,因为它不返回 JavaScript 对象。JSONLint 告诉我这是一个有效的 JSON。

Anyone has an idea of what is happening?

任何人都知道发生了什么?

Thanks

谢谢

EDIT

编辑

I can set to 'json', it is not problem. The problem is that JSON.parse and $.parseJSON should work. Since they are not, I changed 'dataType' to 'json', but the same string is returned. I have no idea what is happening.

我可以设置为'json',没问题。问题是 JSON.parse 和 $.parseJSON 应该可以工作。由于它们不是,我将“dataType”更改为“json”,但返回了相同的字符串。我不知道发生了什么。

回答by Brad Christie

Probably because you're looking for $.parseJSONinstead? Also I beievejQuery will look at the data and make a best-guess at parsing it before passing it off to the callback. So, if it looks like JSON chances are jQuery's already giving you a JavaScript object back which then can'tbe re-parsed using JSON.parse/$.parseJSON.

可能是因为你正在寻找$.parseJSON?此外,我相信jQuery 会查看数据并在将其传递给回调之前对其进行最佳猜测。因此,如果看起来 JSON 很有可能是 jQuery 已经为您提供了一个 JavaScript 对象,然后无法使用JSON.parse/重新解析该对象$.parseJSON

You can also change your dataTypefield to 'json' and let jQuery do it for you...

您还可以将您的dataType字段更改为 'json' 并让 jQuery 为您完成...

回答by Santosh Linkha

change dataType: 'text'to dataType: "json"and also JSON.parseto $.parseJSON

更改dataType: 'text'dataType: "json"并且也更改JSON.parse$.parseJSON

回答by Edgar

The JSONlibrary doesn't exist in all browsers. You might need to include your own like http://developer.yahoo.com/yui/json/

JSON库并非在所有浏览器中都存在。您可能需要像http://developer.yahoo.com/yui/json/一样包含自己的

Or like the others suggested, use the jQuery one. You might also want to declare jsonlike var json = ...

或者像其他人建议的那样,使用 jQuery 一个。您可能还想声明jsonvar json = ...

回答by Gene Bo

In my case, I got it to work as follows:

就我而言,我让它按如下方式工作:

  • from the server (a servlet), I specified json as the data type of the response
  • the jquery call then already parses the response param into a JSON object (so I didn't need to run $.parseJSON) ... thank you https://stackoverflow.com/a/6465506/2162226!
  • 从服务器(一个 servlet),我指定 json 作为响应的数据类型
  • jquery 调用然后已经将响应参数解析为 JSON 对象(所以我不需要运行 $.parseJSON)...谢谢https://stackoverflow.com/a/6465506/2162226

Notice I can: access the json field directly in the response object

注意我可以:直接在响应对象中访问 json 字段

$.ajax({
        type: "POST",
        url: baseUrl,
        dataType: "json",
        data: theData,
        success: function(response) {

                alert(' status = ' + response.status);
                processResponseJSON(response);
        },