I am tyring to add code to get the ipaddress of the user on my site. I am using a react(mobx/mst) with axios.
getIpAddress: flow(function*() {
const response = yield axios.get('http://api.ipify.org/?format=text');
self.ipAddress = response.data;
})
Access to XMLHttpRequest at 'http://api.ipify.org/' from origin 'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource
It's typical CORS issue. Server doesn't allow your client to gather information directly. You should use some server-side proxy or use https://cors-anywhere.herokuapp.com/
, so your code would look like this
getIpAddress: flow(function*() {
const response = yield axios.get('https://cors-anywhere.herokuapp.com/http://api.ipify.org/?format=text');
self.ipAddress = response.data;
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With