I am setting a little website that among other things (importantly for this problem) saves your current location data in a var, which it retrieves from the ipinfo API (https://ipinfo.io/json)
I tried using JSON.parse() which solved the problem of my website displaying "undefined" at the p-tag where the location is normally displayed before redirecting me to url/[object%20Object] after half a second. With parse I get the following error console.log: (see further down)
The JSON response can be used without saving without causing any problems. To see how it looks like click the API link above.
Here is my JS code:
var location;
// called when the document is fully loaded
$(document).ready(function(){
// gets your current location data and displays it
$.get("https://ipinfo.io/json", function(response) {
location = JSON.parse(response);
console.log(location);
document.querySelector('.location').innerHTML = location.city;
});
Here is the HTML:
<h4>You are playing from:</h4>
<div>
<p class="location"></p>
<p class="country"></p>
<p class="region"></p>
</div>
<div class="row">
<div class="column">
<h3>Column 1</h3>
<p id="city_a">
</p>
</div>
<div class="column">
<h3>Column 2</h3>
<p id="city_b">
</p>
</div>
This is the error in the console I get:
VM102:1 Uncaught SyntaxError: Unexpected token o in JSON at position 1
at JSON.parse ()
at Object.success ((index):64)
at u (VM94 jquery-3.3.1.min.js:2)
at Object.fireWith [as resolveWith] (VM94 jquery-3.3.1.min.js:2)
at k (VM94 jquery-3.3.1.min.js:2)
at XMLHttpRequest. (VM94 jquery-3.3.1.min.js:2)
Anything would help :) Thanks in advance!