Making an API call and extracting the Country Code
You can make an API request to IPinfo.io and extract the country
value (E.G - US
) from the response payload like so:
jQuery.ajax({
// remember to add the Token
// and replace the IP address
url: 'https://ipinfo.io/8.8.8.8?token=' + token,
dataType: 'jsonp',
success: function (json) {
alert(json.country);
}
});
Using the filtered response method
The IPinfo API supports filtering API responses by their key/field. You can simply request the field you want by adding it as a sub-URL. Documentation for response filtering.
Code:
jQuery.ajax({
// note the `/country`
url: 'https://ipinfo.io/8.8.8.8/country?token=' + token,
dataType: 'text',
success: function (text) {
alert(text); // the API returns a single string
}
});