User IP address finally resolved

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));

1 Like

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; 
	}); 

});

Hey tony,

I tried ur code. But I am always getting a US ip and address even though I am accessing my site from different country. If I go directly to https://extreme-ip-lookup.com/json , I am getting the correct details. But If I go through the wix fetch api, I am getting wrong details. Do you know any solution for it?

UPDATE: I found what was the issue. I was running ur code in the backend files. Thas why it was showing everytime US address and IP. I moved the code into my site code section and everything is working as expected. Thanks