4

是否可以做出这样的 HTTP 响应,浏览器会忽略它并继续显示先前显示的页面?

我的意思是以下场景:

a) 用户点击某物

b) 一些 POST 发送到服务器(或 GET,但让我们坚持使用 POST 更有趣)

c) 服务器决定出于某种原因此时不想发送回复

d) 服务器发送特制的响应

e) 浏览器不显示错误页面,不显示空白页面,不重定向到任何地方,不重新加载 - 只是继续显示它所显示的内容,就好像用户从未提交过表单一样

PS当然可以用AJAX包装,但我问是否可以使用bare-bones non-javascript解决方案

4

5 回答 5

12

尝试返回 HTTP 204 No Content。请参阅RFC 2616(超文本传输​​协议 -- HTTP/1.1)中此响应代码的描述,其中说:

如果客户端是用户代理,它不应该改变导致请求被发送的文档视图。这个响应主要是为了允许输入动作发生而不导致用户代理的活动文档视图发生变化,尽管任何新的或更新的元信息应该应用于当前在用户代理的活动视图中的文档。

于 2009-03-17T15:08:28.977 回答
1

这是一种通常称为AJAX的方法。它需要JavascriptXMLHttpRequest函数。

编辑:注意到底部的“PS”。另一个答案中建议的204 响应看起来可能是一个有效的非 Javascript 解决方案。

于 2009-03-17T15:08:10.240 回答
1

假设你这样做。

用户做什么?当然,再次提交表格。

几次。

然后他们决定你的网站坏了,然后离开。

于 2009-03-17T15:09:56.960 回答
0

I think that you need Javascript to do that, you can send the request and depending on server response the Javascript can do a return false, avoiding the refresh/reload of the actual page.

I don't know about the 204 response, but maybe that way you can do it from the server side.

Another way of doing it (also Javascript) is to make a return false, right after the request is send, and depending on server response (with ajax) you can modify the actual page or send the user to another page.

于 2009-03-17T15:28:49.660 回答
-2

不,HTTP 协议由一个请求-响应周期组成。服务器不能决定不回复(它可以,但是它破坏了协议并犯了一个错误)。

于 2009-03-17T15:10:11.750 回答