javascript 使用重定向获取删除请求

javascript fetch delete request with redirect

所以我最近一直在为我的 ajax-calls 使用 fetch,发现 DELETE-requests 在我的情况下被重定向 302 实际上一直在发送 delete-requests到服务器重定向到的目标 url。

根据 https://fetch.spec.whatwg.org/#http-redirect-fetch 处的规范,正在重定向的 POST 类型的请求应该使用 GET 来获取即将到来的 url。

所以对于这个问题,delete/put 不应该和 post 一样吗?或者我错过了什么?这是我可以解决的问题,而无需恢复到良好的旧 XMLHttpRequest 或手动处理重定向吗?

将 302 响应的请求类型从 POST 更改为 GET 是旧浏览器存在的错误,现代浏览器继续实施以实现向后兼容性。

由于他们无法发送 DELETE 请求,fetch 正确地实现了 302s 并将它们视为实际的重定向。

由于您要转换为 GET 请求,请改用 303 See Other 响应。 (303和307是​​在HTTP 1.1中引入的,用来处理302的歧义)

The response to the request can be found under another URI using a GET method. When received in response to a POST (or PUT/DELETE), the client should presume that the server has received the data and should issue a redirect with a separate GET message.

https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#3xx_Redirection