sách gpt4 ai đã đi

javascript - 向 https ://newsapi. 组织发出请求时,CORS 策略出现问题

In lại 作者:行者123 更新时间:2023-12-02 16:39:43 34 4
mua khóa gpt4 Nike

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

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

我尝试将 mode: 'no-cors' 添加到提取中,但它不适用于 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' 来自 origin 'null' 已被 CORS 策略阻止:请求的资源上不存在 'Access-Control-Allow-Origin' header 。如果不透明响应满足您的需求,请将请求的模式设置为“no-cors”以获取禁用 CORS 的资源。

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

34 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com