当 Origin 包含端口号时不允许 CORS

Dav*_*d G 5 port cross-domain node.js cors webpack

我有一个在 Node.js 服务器上运行的应用程序。CORS 已启用,但我的 POST 请求被拒绝 ( 400, not allowed)。

当使用 POSTMAN 发送相同的请求时,我只需从Origin. 但它应该可以正常工作,因为请求端口和源端口都是相同的。

例如,这不起作用:

# Request
Request URL: http://the_address:the_port/etc
Request Method: POST

# Headers
Origin: http://the_address:the_port
Run Code Online (Sandbox Code Playgroud)

虽然这工作得很好:

# Headers
Origin: http://the_address
Run Code Online (Sandbox Code Playgroud)

您知道为什么这会破裂吗?即使端口位于源中,只要它是同一端口,CORS 是否不应该允许同源请求?

PS:我也在使用 webpack 和 React,但我不认为它们是相关的(除非我弄错了)。

sid*_*ker 4

您可以\xe2\x80\x99t手动更改Origin浏览器发送的标头。浏览器Origin根据它所知道的您\xe2\x80\x99发送请求的前端JavaScript代码的实际来源来设置它发送的标头。因此,如果您的代码发送请求的 URL 来源\xe2\x80\x99s 与浏览器发送的标头值完全相同Origin,则浏览器会认为这是跨域请求。

\n\n

将前端代码的实际来源更改为http://the_addressfrom的唯一方法http://the_address:the_port是实际提供代码 from http://the_address,其端口号为 80 ,而不是 from http://the_address:the_port,其端口号不是 80 。

\n