Vivek S
Vivek S

Reputation: 5550

Facebook returning data as string, json data is expected

I am using FQL to fetch data from Facebook graph API.When i give a get request via jquery to Facebook, I get the response data in the call back function but,when I tried to process it,i found that the response is treated as a string(I am not able to iterate through it considering it as dictionary) but firebug log shows the following json data.

console.log(response);

RESULT:

{
  "data": {
    "fql_search_result": [{
      "aid": "xxxxx",
    }, {
      "aid": "xxxxx",
    }, ]
  }
}

I got error saying response has no attribute data, when i did this

console.log(response.data)

Why is the data treated as string and not as dictionary?

Upvotes: 0

Views: 261

Answers (2)

Alagappan Ramu
Alagappan Ramu

Reputation: 2368

Did you give the dataType as 'json' in the request that you made via jQuery?

dataType:'json',

Alternatively you can try using the jQuery short hand function for AJAX GET Requests which has dataType as 'json' by default.

jQuery.getJSON("https://graph.facebook.com/fql?q=SELECT+name+FROM+user+WHERE+uid+%3D+me+%28%29&access_token=<access_token>", function(data){
    console.log(data);
    console.log(data.data[0].name);
});

Upvotes: 1

rahul
rahul

Reputation: 7663

try parsing it in json object by using JSON.parse and then access your values

var YourJsonObject = JSON.parse("Your String");

Upvotes: 0

Related Questions