Javascript 新闻API不适用于Chrome,但适用于Firefox
为什么这段代码在Chrome中显示以下错误并在Firefox中正常工作 代码:“corsNotAllowed” 消息:“来自浏览器的请求不会被删除。” 在开发人员计划中允许,本地主机除外。“Javascript 新闻API不适用于Chrome,但适用于Firefox,javascript,api,cors,cors-anywhere,Javascript,Api,Cors,Cors Anywhere,为什么这段代码在Chrome中显示以下错误并在Firefox中正常工作 代码:“corsNotAllowed” 消息:“来自浏览器的请求不会被删除。” 在开发人员计划中允许,本地主机除外。“ Chrome添加了Sec-Fetch-Site:cross-Site标题,但Firefox没有。如果我在Firefox请求中添加Sec Fetch Site:cross Site,则返回错误。因此,newsapi.org似乎会检查此标题,并在“开发者计划”中使用它阻止请求。因此,为了在chrome中使用,我
Chrome添加了
Sec-Fetch-Site:cross-Site
标题,但Firefox没有。如果我在Firefox请求中添加Sec Fetch Site:cross Site
,则返回错误。因此,newsapi.org似乎会检查此标题,并在“开发者计划”中使用它阻止请求。因此,为了在chrome中使用,我们必须使用付费计划吗?或者还有其他方法吗?@SiddhantJain,您不能在JS中更改此标头,因为它是禁止的标头名称,根据返回的消息,在Chrome中使用此服务有一种简单的方法,即使用localhost或使用其他计划。
const proxyUrl = "https://cors-anywhere.herokuapp.com/"
const url =`${proxyUrl}http://newsapi.org/v2/everything?q=bitcoin&from=2020-05-28&sortBy=publishedAt&apiKey=${key}`;
const request = new Request(url);
fetch(request) .then(response => response.json())
.then((news) =>
{ console.log(news); })
.catch(error =>
{ console.log(error); });