Is there a proper way to make an AJAX call when I don't want a response back from the server? I want the server to save some data, but the client doesn't need a response. If the server never responds, will the AJAX connection remain open, waiting for a response until it times out?
当我不希望从服务器返回响应时,是否有正确的方法进行AJAX调用?我希望服务器保存一些数据,但客户端不需要响应。如果服务器永远不响应,AJAX连接是否会保持打开状态,等待响应直到超时?
I would use Javascript like the following. It's a typical AJAX call except I left out the xmlhttp.onreadystatechange event.
我会使用如下的Javascript。这是一个典型的AJAX调用,除了我遗漏了xmlhttp.onreadystatechange事件。
function SaveLabel()
{
var xmlhttp = null;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else if (window.ActiveXObject) {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} else {
alert('Your browser does not support this feature.');
return;
}
xmlhttp.open('GET', 'mypage.aspx?label=1');
xmlhttp.send(null);
}
I found a similar question, but it doesn't really answer mine: Does Ajax connection disconnect at some point?
我发现了一个类似的问题,但它并没有真正回答我的问题:Ajax连接是否在某些时候断开连接?
2 个解决方案
#1
5
You need to keep the connection open until the server is done with it, regardless of whether or not you expect (or will be or won't be) a reply from the server.
无论您是否期望(或将来或将来都是)来自服务器的回复,您都需要保持连接处于打开状态,直到服务器完成。
The reason is that most webservers will kill requests if the client disconnects before the script has finished executing.
原因是如果客户端在脚本执行完毕之前断开连接,大多数Web服务器将终止请求。
The server doesn't have to "respond" as in return data, it just needs to "return".
服务器不必像返回数据那样“响应”,只需要“返回”。
i.e. in answer to your question "Does Ajax connection disconnect at some point?": Yes, when the server terminates the connection.
即回答你的问题“Ajax连接是否在某些时候断开连接?”:是的,当服务器终止连接时。
So what you need to do is make sure that your ASP(X) script terminates as soon as it has finished its work. Don't let it run forever, pass data to a helper daemon/service, etc. that will need long-running processing.
因此,您需要做的是确保ASP(X)脚本在完成其工作后立即终止。不要让它永远运行,将数据传递给需要长时间运行处理的辅助守护程序/服务等。
#2
1
xmlhttp.onreadystatechange
is just an event of the XHR object where you attach a listener to listen for it's changes in state. It's synonymous to having a button on the page and adding an onclick
handler.
xmlhttp.onreadystatechange只是XHR对象的一个事件,您可以在其中附加侦听器以侦听其状态更改。它是在页面上有一个按钮并添加一个onclick处理程序的同义词。
It does the same thing with or without a handler (AJAX still calls, the button is still clickable). The only difference is that you don't know what just happened (You don't know if AJAX succeeded/failed, you don't know when the button was clicked).
无论有没有处理程序,它都会做同样的事情(AJAX仍然会调用,按钮仍然可以点击)。唯一的区别是你不知道刚刚发生了什么(你不知道AJAX是否成功/失败,你不知道点击按钮的时间)。
#1
5
You need to keep the connection open until the server is done with it, regardless of whether or not you expect (or will be or won't be) a reply from the server.
无论您是否期望(或将来或将来都是)来自服务器的回复,您都需要保持连接处于打开状态,直到服务器完成。
The reason is that most webservers will kill requests if the client disconnects before the script has finished executing.
原因是如果客户端在脚本执行完毕之前断开连接,大多数Web服务器将终止请求。
The server doesn't have to "respond" as in return data, it just needs to "return".
服务器不必像返回数据那样“响应”,只需要“返回”。
i.e. in answer to your question "Does Ajax connection disconnect at some point?": Yes, when the server terminates the connection.
即回答你的问题“Ajax连接是否在某些时候断开连接?”:是的,当服务器终止连接时。
So what you need to do is make sure that your ASP(X) script terminates as soon as it has finished its work. Don't let it run forever, pass data to a helper daemon/service, etc. that will need long-running processing.
因此,您需要做的是确保ASP(X)脚本在完成其工作后立即终止。不要让它永远运行,将数据传递给需要长时间运行处理的辅助守护程序/服务等。
#2
1
xmlhttp.onreadystatechange
is just an event of the XHR object where you attach a listener to listen for it's changes in state. It's synonymous to having a button on the page and adding an onclick
handler.
xmlhttp.onreadystatechange只是XHR对象的一个事件,您可以在其中附加侦听器以侦听其状态更改。它是在页面上有一个按钮并添加一个onclick处理程序的同义词。
It does the same thing with or without a handler (AJAX still calls, the button is still clickable). The only difference is that you don't know what just happened (You don't know if AJAX succeeded/failed, you don't know when the button was clicked).
无论有没有处理程序,它都会做同样的事情(AJAX仍然会调用,按钮仍然可以点击)。唯一的区别是你不知道刚刚发生了什么(你不知道AJAX是否成功/失败,你不知道点击按钮的时间)。