Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ipify Getting Blocked By Cors. How do I get it be allowed? [duplicate]

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
like image 774
chobo2 Avatar asked Apr 11 '19 21:04

chobo2


1 Answers

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;  
})
like image 161
viciousP Avatar answered Oct 19 '22 23:10

viciousP