【问题标题】:How to solve the issue of blocking of access to fetch data from yahoo finance by CORS policy?How to solve the issue of blocking of access to fetch data from yahoo finance by CORS policy?
【发布时间】:2022-12-02 00:44:17
【问题描述】:

I am trying to fetch the price of a stock using fetch in my React App. When I try to fetch without options or configurations, using fetch(url), this error comes:

Access to fetch at 'https://query1.finance.yahoo.com/v8/finance/chart/RCF.BO' 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.

The API : https://query1.finance.yahoo.com/v8/finance/chart/<SYMBOL>.BO is open. I don't seem to have any issue fetching it from my browser directly. It's causing the same issue even when I am including this option to the fetch:

    var options = {
      method: 'GET',
      crossorigin: true,
      headers: {
        'Access-Control-Allow-Origin': '*',
      }
    }

The error I am getting as per my knowledge and research is common. But the solutions proposed, deals with changing the server configurations and allowing different origins, which are not applicable for me as the API I am using is open.

Using no-cors also doesn't work as I need the data to be visible so that I can use it. I used some third party extensions while development phase, but now while hosting, it's not fetching the data from the API.

Can someone help with this issue?

【问题讨论】:

    标签: javascript reactjs api fetch


    【解决方案1】:

    You Can't By the sounds of it

    the API your trying to get have there own CORS Policy set which is not allowing you to make the request.

    I hate to be the guy to point you to the docs. But here's the Docs.

    https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS

    【讨论】:

      【解决方案2】:

      The Yahoo page prohibits the query from external via CORS. CORS is a browser-based technology. Outside the browser, e.g. via wget or curl, the page can be queried.

      【讨论】:

        猜你喜欢
        • 2022-12-02
        • 2022-12-27
        • 2022-12-02
        • 2022-12-27
        • 2022-12-01
        • 2022-12-02
        • 2022-12-27
        • 2022-12-01
        • 2022-12-27
        相关资源
        最近更新 更多