I want to get the visitors country based on their IP using javascript. I have made some changes in my html code. I found some piece of code here on stackoverflow which works good, but I don't know how to extract the country from the array.
Code
$.get("https://api.ipdata.co", function (response) {
$("#response").html(JSON.stringify(response, null, 4));
}, "jsonp");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<pre id="response"></pre>
What I want:
I have a pricing table on my html page, now I want to change the price symbole only according to the country of the visitors.
Any help will be highly appreciated.
What about using fetch
and avoiding jQuery altogether?
fetch('https://api.ipdata.co')
.then(res => res.json())
.then(data => console.log(data.country_code));
https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch
Edit: Changes the text value of the element with Id response
to the currency symbol returned.
fetch('https://api.ipdata.co')
.then(res => res.json())
.then(data => {
document.querySelector('#response').textContent = data.currency.symbol;
});