我正在使用POST方法发送Ajax XMLHttpRequest。当请求发送时,我将获得状态为12030的就绪状态4。我知道12030是微软特有的状态代码,它表示连接不持续。然而,我找不到代码出错的地方。而且如果我在不使用Ajax请求的情况下导航到页面,它仍然会加载得很好。下面是javascript方法和调用行。
AJAX方法:
/*Sends ajax request with post data that updates the content view via ajax on completion
* @param message : message after completion of ajax request
* @param url : url to request
* @param params : post parameters as string
*/
function changeAjaxPost(message, url, params) {
var ajx;
if (window.HXMLHttpRequest) {
UtilLogger.log(HtmlLogger.FINE, "Using XMLHttpRequest");
ajx = new XMLHttpRequest();
}
else {
UtilLogger.log(HtmlLogger.FINE, "Using ActiveXObject");
ajx = new ActiveXObject("Microsoft.XMLHTTP");
}
ajx.open("POST", url, true);
ajx.setRequestHeader("X-Requested-With", "XMLHttpRequest");
ajx.setRequestHeader("Content-Type", "text/html");
ajx.setRequestHeader("Content-length", params.length);
ajx.setRequestHeader("Connection", "close");
ajx.send(params);
ajx.onreadystatechange = function () {
document.write(ajx.readyState + ":" + ajx.status);
if (ajx.readyState == 4 && ajx.status == 200) {
alert(message);
updateContent();
}
else if (ajx.readyState == 4 && ajx.status == 400) {
alert("Page Error. Please refresh and try again.");
}
else if (ajx.readyState == 4 && ajx.status == 500) {
alert("Server Error. Please refresh and try again.");
}
}
}
调用行:
changeAjaxPost("Excerpt Saved", "./AJAX/myadditions_content.aspx", params);
Sometimes, using
setRequestHeader("Connection","close");
can cause problems in some browsers.
Removing this solves the problem.
Credit goes to @MikeRobinson
http://danweber.blogspot.com/2007/04/ie6-and-error-code-12030.html
IE6 and error code 12030 If you are running Internet Explorer 6 and using Ajax, you may get some XMLHttpRequests terminated with code 12030. Microsoft's knowledge base at http://support.microsoft.com/kb/193625 shows that this code is
12030 ERROR_INTERNET_CONNECTION_ABORTED The connection with the server has been terminated. Googling turned up no help; the people encountering this don't seem to be aware of how network sockets work, so I had to actually figure it out on my own.
This happens when the client thinks a connection has been kept open, and the server thniks it is closed. The server has sent a FIN, and the client has responded to that with an ACK. Running "netstat" on the Windows client shows that the connection is in the CLOSE_WAIT state, so IE6 really ought to have realized this before trying. This is entirely the client's fault. If you wait about 60 seconds, the Windows OS stack will retire the connection.
If you need to support IE6, you have some solutions, in various degrees of ugly:
retry the ajax request in case of error code 12030 if the browser is ie, send an empty request to the server ahead of each ajax request bundle up your ajax requests such that the time between them is ( (greater than server_timeout) AND (less than server_timeout + one minute) IE7, fwiw, will issue a RST over the CLOSE_WAIT socket as soon as it realizes it has an outgoing connection to make. That, and the socket will only stay in that CLOSE_WAIT state for about 5 seconds anyway.