最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why JSON.parse is not working? - Stack Overflow

programmeradmin2浏览0评论

I set the dataType to 'text' because I don't want to Jquery parse my JSON automatically. 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. 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.

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.

I set the dataType to 'text' because I don't want to Jquery parse my JSON automatically. 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. 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.

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.

Share Improve this question edited Mar 14, 2011 at 15:07 Frias asked Mar 14, 2011 at 14:46 FriasFrias 11.3k9 gold badges35 silver badges40 bronze badges 4
  • Why don't you want jQuery to parse the JSON automatically? – Matt Ball Commented Mar 14, 2011 at 14:57
  • 2 If the parsing is not successful, json would not contain the string, but would be undefined. There must be something else. Sure that json is not an object? What does typeof json give you? Also note that json is a global variable (which it should not be). – Felix Kling Commented Mar 14, 2011 at 14:57
  • What browser you'r using? I tried it with ff3.6 an it worked perfect. try [link]jsfiddle.net/scheffield/URDBv – scheffield Commented Mar 14, 2011 at 15:04
  • It is funny how simple things are some time. The problem was that. json was not a global variable, I just forgot to declare it. And instead JS tell me that is undefined, it though was better to not parse.@Felix Kling – Frias Commented Mar 14, 2011 at 15:20
Add a comment  | 

4 Answers 4

Reset to default 7

Probably because you're looking for $.parseJSON instead? Also I beieve jQuery 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't be re-parsed using JSON.parse/$.parseJSON.

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

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

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

Or like the others suggested, use the jQuery one. You might also want to declare json like var 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
  • 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 !

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

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

                alert(' status = ' + response.status);
                processResponseJSON(response);
        },
发布评论

评论列表(0)

  1. 暂无评论