Reputation: 11281
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?
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.
Upvotes: 7
Views: 32932
Reputation: 12073
In my case, I got it to work as follows:
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);
},
Upvotes: 0
Reputation: 101614
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...
Upvotes: 7
Reputation: 14318
change dataType: 'text'
to dataType: "json"
and also JSON.parse
to $.parseJSON
Upvotes: 2
Reputation: 4488
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 = ...
Upvotes: 1