简体   繁体   English

为什么JSON.parse不起作用?

[英]Why JSON.parse is not working?

I set the dataType to 'text' because I don't want to Jquery parse my JSON automatically. 我将dataType设置为'text',因为我不想让Jquery自动解析我的JSON。 My code is the following: 我的代码如下:

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. 它在两个console.log命令中返回字符串:{“error”:[“ORA-01031权限不足”]}。 It means that the parse isn't working since it doesn't return a JavaScript object. 这意味着解析不起作用,因为它不返回JavaScript对象。 JSONLint says to me that is a valid JSON. JSONLint对我说这是一个有效的JSON。

Anyone has an idea of what is happening? 任何人都知道发生了什么?

Thanks 谢谢

EDIT 编辑

I can set to 'json', it is not problem. 我可以设置'json',这不是问题。 The problem is that JSON.parse and $.parseJSON should work. 问题是JSON.parse和$ .parseJSON应该可以工作。 Since they are not, I changed 'dataType' to 'json', but the same string is returned. 由于它们不是,我将'dataType'更改为'json',但返回相同的字符串。 I have no idea what is happening. 我不知道发生了什么。

Probably because you're looking for $.parseJSON instead? 可能是因为你正在寻找$.parseJSON而不是? Also I beieve jQuery will look at the data and make a best-guess at parsing it before passing it off to the callback. 另外我认为 jQuery会查看数据并在解析之前做出最佳猜测,然后再将其传递给回调。 So, if it looks like JSON chances are jQuery's already giving you a JavaScript object back which then can't be re-parsed using JSON.parse / $.parseJSON . 所以,如果它看起来像JSON的机会是jQuery已经给你一个JavaScript对象,然后无法使用JSON.parse / $.parseJSON重新解析。

You can also change your dataType field to 'json' and let jQuery do it for you... 您还可以将dataType字段更改为'json',让jQuery为您执行此操作...

dataType: 'text'更改为dataType: "json"以及JSON.parse更改$.parseJSON

The JSON library doesn't exist in all browsers. 所有浏览器中都不存在JSON库。 You might need to include your own like http://developer.yahoo.com/yui/json/ 你可能需要包括你自己的http://developer.yahoo.com/yui/json/

Or like the others suggested, use the jQuery one. 或者像其他人建议的那样,使用jQuery。 You might also want to declare json like var json = ... 您可能还想声明jsonvar json = ...

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 从服务器(servlet),我指定json作为响应的数据类型
  • 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 ! 然后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);
        },

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

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