Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Dot Notation = Undefined

Dot notation allows for accessing objects with a '.' Cannot figure out why this is happening. I have the following success function, as part of a jQuery $.ajax function.

success: function(data){
  console.log('data = ' + data);
  console.log('data.president = ' + data.president);
  console.log('data.adviser = ' + data.adviser);
}

This, oddly, results in the following browser log:

data = {"president":1,"adviser":1}
data.president = undefined
data.adviser = undefined

I must be missing something painfully obvious. Can somebody enlighten me?

like image 815
cdwyer Avatar asked Dec 14 '22 08:12

cdwyer


1 Answers

The data would have to be an Object to be accessed by a dot .. It's a string now. You need to parse it using for example:

data = JSON.parse(data);
like image 51
jdabrowski Avatar answered Dec 17 '22 01:12

jdabrowski