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

        <legend id='64W7i'><style id='64W7i'><dir id='64W7i'><q id='64W7i'></q></dir></style></legend>
        <tfoot id='64W7i'></tfoot>
          <bdo id='64W7i'></bdo><ul id='64W7i'></ul>

        <small id='64W7i'></small><noframes id='64W7i'>

      1. 向 https://newsapi.org 发出请求时,CORS 政策出现问题

        时间:2023-10-14
          <i id='10WBX'><tr id='10WBX'><dt id='10WBX'><q id='10WBX'><span id='10WBX'><b id='10WBX'><form id='10WBX'><ins id='10WBX'></ins><ul id='10WBX'></ul><sub id='10WBX'></sub></form><legend id='10WBX'></legend><bdo id='10WBX'><pre id='10WBX'><center id='10WBX'></center></pre></bdo></b><th id='10WBX'></th></span></q></dt></tr></i><div id='10WBX'><tfoot id='10WBX'></tfoot><dl id='10WBX'><fieldset id='10WBX'></fieldset></dl></div>

          <small id='10WBX'></small><noframes id='10WBX'>

                <tbody id='10WBX'></tbody>

            1. <tfoot id='10WBX'></tfoot>

                  <bdo id='10WBX'></bdo><ul id='10WBX'></ul>

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

                  问题描述

                  我一直在我的网站上运行新闻 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 政策出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:hapi.js Cors Pre-flight 不返回 Access-Control-Allow-Origin 下一篇:跨域 Ajax 请求在 Opera 和 IE9 中不起作用?

                  相关文章

                  最新文章

                    <bdo id='bIgnY'></bdo><ul id='bIgnY'></ul>

                    <tfoot id='bIgnY'></tfoot>

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

                    3. <small id='bIgnY'></small><noframes id='bIgnY'>