Why doesn't JSON.parse work?

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

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 this line: {"error": ["ORA-01031 Invalid privileges"]} in both console.log commands. This means that parsing does not work because it does not return a JavaScript object. JSONLint tells me that it is valid JSON.

Does anyone have an idea of ​​what is going on?

thanks

EDIT

I can install 'json', this is not a problem. The problem is that JSON.parse and $ .parseJSON should work. Since this is not the case, I changed 'dataType' to 'json', but the same line is returned. I have no idea what is going on.

+7
source share
4 answers

Perhaps because you are looking for $.parseJSON instead? I also believe that jQuery will look at the data and do everything possible to parse it before passing it to the callback. So, if that looks like a JSON probability, jQuery already gives you a JavaScript object that then cannot be reprocessed with JSON.parse / $.parseJSON .

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

+7
source

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

+2
source

The JSON library does not exist in all browsers. You may need to include your own, for example http://developer.yahoo.com/yui/json/

Or, like others, use jQuery. You can also declare JSON as var json = ...

+1
source

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

  • from the server (servlet), I specified json as the response data type
  • the jquery call then already parses the response parameter in the JSON object (so I don’t have to run $ .parseJSON) ... thanks https://stackoverflow.com/a/2166168/167

Note that I can: access json fields directly in the response object

 $.ajax({ type: "POST", url: baseUrl, dataType: "json", data: theData, success: function(response) { alert(' status = ' + response.status); processResponseJSON(response); }, 
0
source

All Articles