POST 上的 CORS 错误,没有内容

Posted

技术标签:

【中文标题】POST 上的 CORS 错误,没有内容【英文标题】:CORS Error on POST with no content 【发布时间】:2018-07-26 07:33:15 【问题描述】:

我有一个控制器样式的 REST Web 服务端点,如果我发布非空内容,它将在浏览器中工作,但是当内容为空时,浏览器返回:

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 403. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

这是一个内容有效的请求:

POST /my/controller/resource HTTP/1.1
Host: localhost:8083
Connection: keep-alive
Content-Length: 1
accept: application/json
Origin: null
Authorization: Bearer JWT
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/63.0.3239.132 Safari/537.36
Content-Type: application/json
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9

HTTP/1.1 204 No Content
Server: Apache-Coyote/1.1
Access-Control-Allow-Origin: null
Access-Control-Allow-Credentials: true
Content-Language: en-US
Date: Thu, 15 Feb 2018 13:44:53 GMT

这是一个没有内容的请求,不起作用

POST /my/controller/resource HTTP/1.1
Host: localhost:8083
Connection: keep-alive
Content-Length: 0
accept: application/json
Origin: null
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36
Authorization: Bearer JWT
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9

HTTP/1.1 403 Forbidden
Server: Apache-Coyote/1.1
Content-Type: text/plain
Content-Length: 0
Date: Thu, 15 Feb 2018 13:42:16 GMT

谁能解释为什么没有内容的请求不起作用 - 以及我可以做些什么来纠正这个问题?

【问题讨论】:

【参考方案1】:

这似乎不是 CORS 错误 - 服务器设置为不允许零内容长度的 POST 请求。这就是抛出 403 错误响应的原因。

【讨论】:

以上是关于POST 上的 CORS 错误,没有内容的主要内容,如果未能解决你的问题,请参考以下文章

为啥 CORS 中没有针对具有标准内容类型的 POST 请求的预检

即使没有发出预检请求,也会在 POST 请求中出现 CORS 错误

获取无效 POST、PATCH 的 CORS 错误 - REST 端点

浏览器向 django 发出 CORS 请求的 OPTIONS 请求,但没有 POST

React Express Fetch Post CORS 错误:对预检请求的响应未通过访问控制检查:它没有 HTTP ok 状态

Cors Post 请求上的 Axios 网络错误,状态码为 200