gpt4 book ai didi

javascript - 如何防止 Backbone 和 Node.js/Hapi 出现 CORB 错误?

转载 作者:行者123 更新时间:2023-12-03 00:03:58 24 4
gpt4 key购买 nike

过去几天对 Chrome 的更新导致从我的 SPA (Backbone.js) 到我的服务器(运行 Hapi 的 Node.js)的一些 API 调用因 CORB 错误而被阻止。我正在进行原型(prototype)设计,因此当前没有进行身份验证。

在主干中,我使用模型 url 属性,但没有找到指定 header 和有效负载类型的方法。

url: function () {
return 'http://localhost:4000/api/getSpotPrices/' + energy.type);
}

我是否需要更改 Hapi 中的设置来防止这种情况发生?

[更新]我在 Hapi 配置中添加了 CORS 设置并将其设置为通配符:

server.route({
config: {
cors: {
origin: ['*']
}
},
method: 'GET',
etc...
}

现在 Chrome 抛出错误:

The 'Access-Control-Allow-Origin' header contains multiple values 'http://localhost:63342, *', but only one is allowed.

因此,如果没有 CORS 配置,CORB 会阻止我的服务调用。通过 CORS 配置设置,CORS 会提示 header 条目中的条目过多。

最佳答案

您的请求中是否有任何其他 header 。这可能会导致此问题。

这是我在 React 前端中使用的 cors 配置。

cors: {
origin: ['list of domains that white listed, no need for wildcard for me'],
credentials: true, // for preflight request
// these are the additional headers that i am using through my client code
additionalHeaders: ['cache-control', 'x-requested-with', 'x-csrf-token', 'set-cookie']
},

关于javascript - 如何防止 Backbone 和 Node.js/Hapi 出现 CORB 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55053999/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com