如何返回“noop”通过 HTTP 传输文档

发布于 2024-10-08 20:28:20 字数 487 浏览 0 评论 0原文

我有一个可以做很多事情的 CGI 脚本。我试图使脚本保持非常简单和可移植。我只需要某种方式让用户向服务器发送消息而不必丢失当前页面。到目前为止,我有以下过程:

  1. 用户从 CGI 脚本加载“状态页面”。
  2. 状态页面有一个 JavaScript 计时器,每两秒从服务器读取数据(不是整个页面)并相应地更改状态页面。
  3. 用户单击超链接元素以在服务器上启动作业。
  4. CGI 接收来自单击的参数并启动作业。
  5. CGI 发送 \n 响应

此时 Firefox 会询问用户是否要下载 CGI 脚本,当然该脚本只是 CGI 发送的 \n发送。相反,我希望浏览器完全忽略响应。如果我的 CGI 脚本没有回显 \n apache 会给出错误。我该怎么做才能告诉浏览器忽略响应并留在当前页面?请注意,我不想重新加载当前页面。我认为对于这种情况必须有某种“noop”HTTP 响应。

I have a CGI script that does a lot things. I'm trying to keep the script very simple and portable. I just need some way for the user to send a message to the server without having to lose the current page. So far, I have the following process:

  1. User loads a "status page" from a CGI script.
  2. The status page has a javascript timer that will read data (not the entire page) from the server every two seconds and alter the status page accordingly.
  3. User clicks a hyperlink element to launch a job on the server.
  4. The CGI receives the parameters from the click and starts the job.
  5. The CGI sends a response of \n

At this point Firefox asks the user if they want to download the CGI script and of course the script is just the \n that the CGI sent. Instead, I want the browser to ignore the response altogether. If my CGI script does not echo a \n apache gives an error. What could I do to tell the browser to ignore the response and stay on the current page? Note that I would rather not reload the current page. I'm thinking there must be some sort of "noop" HTTP response for such a case.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

裸钻 2024-10-15 20:28:20

发回带有 204 HTTP 状态代码的响应。来自 RFC 2616 又名超文本传输​​协议 - HTTP/1.1

10.2.5 204 无内容

服务器已完成请求
但不需要返回
实体主体,并且可能想要返回
更新的元信息。回应
可能包括新的或更新的
元信息的形式
实体标题,如果存在的话
应与
请求的变体。

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

204 响应不得包含
消息体,因此总是
由第一个空行终止
在标头字段之后。

Send back a response with the 204 HTTP status code. From RFC 2616 aka Hypertext Transfer Protocol -- HTTP/1.1:

10.2.5 204 No Content

The server has fulfilled the request
but does not need to return an
entity-body, and might want to return
updated metainformation. The response
MAY include new or updated
metainformation in the form of
entity-headers, which if present
SHOULD be associated with the
requested variant.

If the client is a user agent, it
SHOULD NOT change its document view
from that which caused the request to
be sent. This response is primarily
intended to allow input for actions to
take place without causing a change to
the user agent's active document view,
although any new or updated
metainformation SHOULD be applied to
the document currently in the user
agent's active view.

The 204 response MUST NOT include a
message-body, and thus is always
terminated by the first empty line
after the header fields.

简美 2024-10-15 20:28:20

您可能不想尝试在服务器端解决此问题,而是想研究客户端解决方案。例如,使用 jQuery 您可以轻松启动 单击按钮时向服务器发出 AJAX 异步请求。您根本不必在浏览器上加载新页面。

Instead of trying to solve this problem on the server side, you might want to investigate a client side solution. For example, using jQuery you can easily initiate an AJAX asynchronous request to the server on a button click. You don't have to load a new page on the browser at all.

梦里°也失望 2024-10-15 20:28:20

Instead of having the hyperlink be a real <a> or <form> with default behavior, have it be some clickable element whose clicks are handled by your client-side code. The Javascript code should send the job requests with XMLHttpRequest objects, putting it in complete control of how the response is handled.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文