1. <i id='x13S8'><tr id='x13S8'><dt id='x13S8'><q id='x13S8'><span id='x13S8'><b id='x13S8'><form id='x13S8'><ins id='x13S8'></ins><ul id='x13S8'></ul><sub id='x13S8'></sub></form><legend id='x13S8'></legend><bdo id='x13S8'><pre id='x13S8'><center id='x13S8'></center></pre></bdo></b><th id='x13S8'></th></span></q></dt></tr></i><div id='x13S8'><tfoot id='x13S8'></tfoot><dl id='x13S8'><fieldset id='x13S8'></fieldset></dl></div>

      <legend id='x13S8'><style id='x13S8'><dir id='x13S8'><q id='x13S8'></q></dir></style></legend>

        <bdo id='x13S8'></bdo><ul id='x13S8'></ul>
    1. <small id='x13S8'></small><noframes id='x13S8'>

      <tfoot id='x13S8'></tfoot>
    2. 向 https://newsapi.org 发出请求时,CORS 政策出现问题

      Problem with CORS policy, when making a request to https://newsapi.org(向 https://newsapi.org 发出请求时,CORS 政策出现问题)

        <small id='xrpX6'></small><noframes id='xrpX6'>

          <tbody id='xrpX6'></tbody>
        • <bdo id='xrpX6'></bdo><ul id='xrpX6'></ul>
            <i id='xrpX6'><tr id='xrpX6'><dt id='xrpX6'><q id='xrpX6'><span id='xrpX6'><b id='xrpX6'><form id='xrpX6'><ins id='xrpX6'></ins><ul id='xrpX6'></ul><sub id='xrpX6'></sub></form><legend id='xrpX6'></legend><bdo id='xrpX6'><pre id='xrpX6'><center id='xrpX6'></center></pre></bdo></b><th id='xrpX6'></th></span></q></dt></tr></i><div id='xrpX6'><tfoot id='xrpX6'></tfoot><dl id='xrpX6'><fieldset id='xrpX6'></fieldset></dl></div>

                <legend id='xrpX6'><style id='xrpX6'><dir id='xrpX6'><q id='xrpX6'></q></dir></style></legend>
                <tfoot id='xrpX6'></tfoot>
              1. 本文介绍了向 https://newsapi.org 发出请求时,CORS 政策出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                我一直在我的网站上运行新闻 api 并通过将文件拖到网络浏览器中在我的计算机上进行测试,网址会显示为 file:///C:.然后我会将任何更改上传到我的 GitHub 存储库并在 Github 页面 https://name.github.io/repository/ 上运行它.

                I have been running news api on my website and testing in on my computer by dragging the file into the web browser, the url would show up like that file:///C:. Then I would upload any changes to my GitHub repository and run it on Github pages https://name.github.io/repository/.

                长期以来一切正常,但最终,API 停止工作,控制台中出现错误 Access to fetch at 'https://newsapi.org/v2/everything?xx' from originhttps://name.github.io"已被 CORS 策略阻止:请求的资源上不存在Access-Control-Allow-Origin"标头.如果不透明的响应满足您的需求,请将请求的模式设置为no-cors"以获取禁用 CORS 的资源.

                Everything was working fine for a long time, but eventually, the API stopped working and error showed up in the console Access to fetch at 'https://newsapi.org/v2/everything?xx' from origin 'https://name.github.io' has been blocked by CORS policy: 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.

                我尝试将 mode: 'no-cors' 添加到 fetch 中,但它不适用于 return response.json();

                I have tried to add mode: 'no-cors' to the fetch, but it didn't work with return response.json();

                我的函数如下所示:

                  const url = 'https://newsapi.org/v2/everything?' +
                    'qInTitle=""&' +
                    `from=` +
                    'language=en&' +
                    'apiKey=';
                  const req = new Request(url);
                
                  fetch(req).then(function(response) {
                    return response.json();
                  }).then(function(news) {
                    newsLoop(news);
                  });
                

                当我在本地运行它时,API 也停止工作 file:///C:,它显示与 Github 页面上的错误类似的错误 Access to fetch at 'https://newsapi.org/v2/everything?xx' from origin 'null' 已被 CORS 策略阻止:请求的资源上不存在Access-Control-Allow-Origin"标头.如果不透明的响应满足您的需求,请将请求的模式设置为no-cors"以获取禁用 CORS 的资源.

                The API stopped working also when I run it locally file:///C:, it displays a similar error to the one on Github pages Access to fetch at 'https://newsapi.org/v2/everything?xx' from origin 'null' has been blocked by CORS policy: 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.

                我该如何处理,以便 API 会在 Github 页面上显示信息,以及当我在我的电脑上本地运行它时?

                How I can deal with it, so the API would display information on Github pages and when I run it locally on my pc?

                推荐答案

                你需要一个 CORS 代理

                You need a CORS proxy

                const proxyUrl = "https://cors-anywhere.herokuapp.com/"
                const qInTitle = "";
                const from = "";
                const apiKey = "";
                const url = `${proxyUrl}https://newsapi.org/v2/everything?qInTitle=${qInTitle}&from=${from}language=en&apiKey=${apiKey}`;
                const request = new Request(url);
                
                fetch(request)
                  .then(response => response.json())
                  .then((news) => {
                    console.log(news);
                  })
                  .catch(error => {
                    console.log(error);
                  });

                这篇关于向 https://newsapi.org 发出请求时,CORS 政策出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                SCRIPT5: Access is denied in IE9 on xmlhttprequest(SCRIPT5:在 IE9 中对 xmlhttprequest 的访问被拒绝)
                XMLHttpRequest module not defined/found(XMLHttpRequest 模块未定义/未找到)
                Show a progress bar for downloading files using XHR2/AJAX(显示使用 XHR2/AJAX 下载文件的进度条)
                How can I open a JSON file in JavaScript without jQuery?(如何在没有 jQuery 的情况下在 JavaScript 中打开 JSON 文件?)
                quot;Origin null is not allowed by Access-Control-Allow-Originquot; in Chrome. Why?(“Access-Control-Allow-Origin 不允许 Origin null在铬.为什么?)
                How to get response url in XMLHttpRequest?(如何在 XMLHttpRequest 中获取响应 url?)

                    <tbody id='dBsRI'></tbody>
                    <legend id='dBsRI'><style id='dBsRI'><dir id='dBsRI'><q id='dBsRI'></q></dir></style></legend>
                  1. <tfoot id='dBsRI'></tfoot>
                    • <bdo id='dBsRI'></bdo><ul id='dBsRI'></ul>

                      <small id='dBsRI'></small><noframes id='dBsRI'>

                        1. <i id='dBsRI'><tr id='dBsRI'><dt id='dBsRI'><q id='dBsRI'><span id='dBsRI'><b id='dBsRI'><form id='dBsRI'><ins id='dBsRI'></ins><ul id='dBsRI'></ul><sub id='dBsRI'></sub></form><legend id='dBsRI'></legend><bdo id='dBsRI'><pre id='dBsRI'><center id='dBsRI'></center></pre></bdo></b><th id='dBsRI'></th></span></q></dt></tr></i><div id='dBsRI'><tfoot id='dBsRI'></tfoot><dl id='dBsRI'><fieldset id='dBsRI'></fieldset></dl></div>