如何返回“noop”通过 HTTP 传输文档
我有一个可以做很多事情的 CGI 脚本。我试图使脚本保持非常简单和可移植。我只需要某种方式让用户向服务器发送消息而不必丢失当前页面。到目前为止,我有以下过程:
- 用户从 CGI 脚本加载“状态页面”。
- 状态页面有一个 JavaScript 计时器,每两秒从服务器读取数据(不是整个页面)并相应地更改状态页面。
- 用户单击超链接元素以在服务器上启动作业。
- CGI 接收来自单击的参数并启动作业。
- 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:
- User loads a "status page" from a CGI script.
- 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.
- User clicks a hyperlink element to launch a job on the server.
- The CGI receives the parameters from the click and starts the job.
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
发回带有 204 HTTP 状态代码的响应。来自 RFC 2616 又名超文本传输协议 - HTTP/1.1:
Send back a response with the 204 HTTP status code. From RFC 2616 aka Hypertext Transfer Protocol -- HTTP/1.1:
您可能不想尝试在服务器端解决此问题,而是想研究客户端解决方案。例如,使用 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.
不要让超链接成为具有默认行为的真正的
或
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.