“成功与错误”的HTTP状态代码? [英] HTTP status code for "success with errors"?
问题描述
我已经戳了一下,但是当请求成功时我没有看到HTTP状态代码,但是在不返回点之后出现错误。
I've poked around a bit, but I don't see an HTTP status code for when a request's succeeds, but there is an error after the "point of no return".
例如,假设你处理一个请求,它已经提交给数据库,但是在返回结果时你运行了内存,或者遇到了NPE,或者你有什么。它将一个 200
响应,但现在,在内部,您无法返回正确,格式正确的响应。
e.g., Say you process a request, its committed to the database, but while returning the result you run of memory, or encounter a NPE, or what have you. It would have been a 200
response, but now, internally, you aren't able to return the proper, well-formed response.
202接受
似乎不合适,因为我们已经处理了请求。
202 Accepted
doesn't seem to fit since we've already processed the request.
什么状态代码意味着成功,但错误?是否存在一个?
What status code means "Success, but errors"? Does one even exist?
推荐答案
HTTP没有这样的状态代码,但有一种最佳做法可以让你处理这种情况 - 在POST操作后重定向用户。
HTTP doesn't have such a status code, but there is a best practice that allows you to handle such situations - redirect the user after a POST operation.
这是一个分解 -
- POST请求尝试修改服务器上的数据
- 如果服务器出现故障,则会发送500错误以指示失败
- 如果服务器成功,它会发送302重定向响应
- 浏览器然后向服务器发送新的GET请求
- 如果此操作失败,您将获得一个500错误,否则你得到一个200
- A POST request tries to modify data on the server
- If the server fails, it sends a 500 error to indicate failure
- If the server succeeds, it sends a 302 redirect response
- The browser then sends a fresh GET request to the server
- If this fails, you get a 500 error, otherwise you get a 200
所以,你的保存数据但无法立即检索的用例翻译对于初始POST的302重定向,接着是后续GET的500重定向。
So, your use case of 'Saved data but can't retrieve it immediately' translates to a 302 redirect for the initial POST, followed by a 500 for the subsequent GET.
这种方法还有其他优点 - 你摆脱了烦人的'你确定你想要重新提交数据?'消息。还可以使你的后退/前进/刷新按钮可用。
This approach has other advantages - you get rid of the annoying 'Are you sure you want to resubmit the data?' message. Also keeps your back/forward/refresh buttons usable.
这篇关于“成功与错误”的HTTP状态代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!