It took me a while trying the Wix native solution with the Routers unsuccessfully.
We finally resorted for a simple JSON query (below). Replace IP API with the site of your choice and json.query with the variable you want to retrieve.
M y issue with IP API is that the variables are between " ", unlike other sites (but I find them the most accurate). Any advice on how to get around this in Wix?
import {fetch} from 'wix-fetch';
fetch('https://ipinfo.io/json', {method: 'get'})
.then( (httpResponse) => {
if (httpResponse.ok) {
return httpResponse.json();
} else {
return Promise.reject("Fetch did not succeed");
}
} )
.then(json => $w('#text1').text = json.query)
.catch(err => console.log(err));
UPDATE: here is a working code:
import {
fetch
}
from 'wix-fetch';
$w.onReady(function () {
fetch('https://extreme-ip-lookup.com/json', {
method: 'get'
})
.then((httpResponse) => {
if (httpResponse.ok) {
return httpResponse.json();
}
})
.then((json) => {
const ipaddress = json.query;
console.log(ipaddress);
return ipaddress;
});
});