ReactJS: No 'Access-Control-Allow-Origin' header with XMLHttpRequest

后端 未结 2 1184
耶瑟儿~
耶瑟儿~ 2021-01-23 02:54

The code below gives the next error. I\'ve set the header. I found the problem lies in CORS but couldn\'t find how to make it work. In the commented I tried to send request with

相关标签:
2条回答
  • 2021-01-23 03:10

    The easiest way to do this, since you have no control of the data that is being send by the server is to use cors-anywhere. It adds CORS to the data that is being send from the server.

    There is a preconfigured heroku app for cors-anywhere at this link.

    You can change your axios call to

      axios.get('https://cors-anywhere.herokuapp.com/https://picasaweb.google.com/data/feed/api/user/' + response.googleId + '?kind=album&access=public', config)
       .then(function(response2){
         console.log(response2);
       })
       .catch(function(error){
         console.log(error);
       });
    

    so that you'll get a CORS header to the response.

    0 讨论(0)
  • 2021-01-23 03:14

    You need to allow the whitelist header "http://localhost:3000" on you Google developer account.

    or

    Try changing the dataType: 'jsonp' in your ajax call.

    0 讨论(0)
提交回复
热议问题