Kelly o'Brian
Kelly o'Brian

Reputation: 445

Consuming a JSON API response using JQuery

I came out with the following to read the IP of the client connecting to my server. It works fine, however I can read the single "ip" value from the returned data structure. Must be something basic, but I never work with jQuery/Javascript...

$.getJSON('https://ipapi.co/json/', function(data) {
  var data = (JSON.stringify(data, null, 2));
  console.log(data);
  var ip = data['ip'];
  console.log(ip);
});

Upvotes: 0

Views: 69

Answers (1)

antoniom
antoniom

Reputation: 3241

Just erase the following line

var data = (JSON.stringify(data, null, 2));

getJSON will automatically parse the data for you and you don't want to cast your data to string

Consider however to chain with a .fail() in case something goes wrong

Upvotes: 2

Related Questions