Lucfia Lucfia - 5 months ago 17
Javascript Question

Retrieve data from jQuery Object

I am having problems with my jQuery object which I recieve from Ajax function.

console.log(data) ==> dumps {"isUnique":true}


and

console.log(data.isUnique) ==> dumps undefined


How should I get these "isUnique" data?

Code
Js:

$.ajax({
type: "POST",
async: false,
url: ...some url ,
data: { 'email' : value },
dataType: "text",
success: function(data){
console.log(data);
console.log(data.isUnique);
}
});

Answer

Your dataType is incorrect

Change

dataType: "text",

To

dataType: "json",

This will tell jQuery to parse the response to an object from string internally