17

I am currently using yahoo weather API to fetch weather data. I am getting below error. Please help.

Access to fetch at 'https://weather-ydn-yql.media.yahoo.com/forecastrss?location=sunnyvale,ca&format=json' from origin 'http://localhost:3000' 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. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

I have tried to do google, but none of solution worked for me

shA.t
  • 16,580
  • 5
  • 54
  • 111
Neel Shah
  • 179
  • 1
  • 1
  • 4

2 Answers2

43

You can set the request mode to 'no-cors' like so:

fetch(url, {
    mode: "no-cors",
    ...
})

But, as goto1 described in the comment below, it will usually not be the solution you are looking for:

@goto1

If you're getting CORS issues this is not the solution. If the API endpoint is something that you control, then you have to fix those CORS issue there. If the API endpoint is not something that you control, then you have to proxy your request through a server that does have CORS enabled.

rmcsharry
  • 5,363
  • 6
  • 65
  • 108
yagiro
  • 737
  • 5
  • 7
  • 10
    If you're getting `CORS` issues **this is not** the solution. If the `API` endpoint is something that you control, then you have to fix those `CORS` issue there. If the `API` endpoint is not something that you control, then you have to `proxy` your request through a server that does have `CORS` enabled. – goto Jun 21 '20 at 18:55
  • A description of the fetch api's cors behavior and settings: https://javascript.info/fetch-crossorigin – txyoji Oct 28 '20 at 15:55
6

No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled

So, First of all you have to change your CORS from browser : Here is the Link of that , download it and it will install by it self.

visit:https://chrome.google.com/webstore/detail/moesif-origin-cors-change/digfbfaphojjndkpccljibejjbppifbc?hl=en-US

Name : Moesif Origin & CORS Changer

NOTE: Make sure it turns ON while you running your page.

Nishith Darji
  • 301
  • 3
  • 5