javascript 当服务器没有响应时如何处理ajax调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10395060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to handle ajax call when there is no response from the server
提问by Alon
I have an ajax call for some data (using jQuery). after the user clicks "submit" (and the ajax call has been sent) I am displaying a "Please wait..." message that disables everything until the request returns (so the user won't double click or click other things and mess things up).
我有一些数据的 ajax 调用(使用 jQuery)。在用户单击“提交”(并且已发送 ajax 调用)后,我将显示“请稍候...”消息,该消息禁用所有内容,直到请求返回(因此用户不会双击或单击其他内容并弄乱事情了)。
It works great when there is any kind of error - the "Please wait..." disappears and I am displaying the user what went wrong.
当出现任何类型的错误时,它工作得很好 - “请稍候...”消失,我正在向用户显示出了什么问题。
But what happens if the server don't return me anything back because of communication error?
但是如果服务器因为通信错误而没有返回任何东西会发生什么?
The solution I found for that is to set a timeout of 10 seconds for the "Please wait.."message that after that time it disappears and displays and error that "The communication failed". I assume that if the server didn't respond after 10 seconds then it will not respond at all - but that it false assumption.
我为此找到的解决方案是为“请稍候..”消息设置 10 秒的超时时间,在此之后它会消失并显示“通信失败”的错误消息。我假设如果服务器在 10 秒后没有响应,那么它根本不会响应 - 但这是错误的假设。
The problem is- how can I be sure that after 20 seconds the server won't return something back? The scenario that might happen is that the user click submits --> 10 seconds later he get an error message --> 5 seconds later server response and confuses the user
问题是- 我如何确定 20 秒后服务器不会返回某些内容?可能发生的情况是用户单击提交 --> 10 秒后他收到错误消息 --> 5 秒后服务器响应并使用户感到困惑
How do I make sure that after I hide the "Please wait.." message nothing will pop up from the server?
如何确保在隐藏“请稍候..”消息后,服务器不会弹出任何内容?
采纳答案by Parv Sharma
when you send a request to a server. a connection is opened and its kept open unless the server responds.
1.if due to some error on the server side it cannot respond then a response code of 5xx is sent back generally (503)
2.if due to some connection issues the connection is terminated prematurely then also jquery would take that as an error.
1.so if you wanna wait for the server to send a request or connection termination (which ever occurs earlier) then u can use the completed
option in the jquery ajax
.
2.and if you are in a condition in which server isnt responding even after 20 secs and you think that it should have responded by now use timeout.
3.finally if your problem is that you are using some kind of customized(hand made http server) which doesn't end a request even if it encounters some error then atleast customize it enough so that it sends back some response code(because this is HTTP model of request and response)
当您向服务器发送请求时。除非服务器响应,否则连接已打开并保持打开状态。
1.如果由于服务器端的某些错误它无法响应,那么通常会发回 5xx 的响应代码 (503)
2.如果由于某些连接问题,连接过早终止,那么 jquery 也会将其视为错误。
1.so如果你想等待服务器发送请求或连接终止(这曾经出现得更早),那么在哪里使用completed
的选项jquery ajax
。
2.如果您处于服务器即使在 20 秒后也没有响应并且您认为它现在应该已经响应的情况,请使用超时。
3.最后,如果您的问题是您使用的是某种定制的(手工制作的 http 服务器),即使遇到一些错误也不会结束请求,那么至少对其进行足够的定制,以便它发回一些响应代码(因为这是请求和响应的 HTTP 模型)
回答by coder
You can handle something like this
你可以处理这样的事情
if ( request.readyState == 4 ){ // 4 is "complete"
if ( request.status == 200 ){
// HTTP OK, carry out your normal Ajax processing
// ...
}else{
// something went wrong, report the error
error( "HTTP "+request.status+". An error was ?
encountered: "+ request.statusText );
}
}
(or)
(或者)
$.ajax({
type: "post",
url: "somepage.html",
success: function (data, text) {
//...
},
error: function (request, status, error) {
alert(request.responseText);
}
});
回答by DCoder
- Generate a unique token when you fire a request,
- keep a list of valid tokens,
- remove tokens when the request times out/fails,
- check if token is still valid before executing success/error callbacks.
- 发出请求时生成唯一的令牌,
- 保留有效令牌列表,
- 当请求超时/失败时删除令牌,
- 在执行成功/错误回调之前检查令牌是否仍然有效。
The same pattern can be adapted for a situation when you need to send frequent repeating requests (e.g. autocompletion/result filtering) and only the latest one's handler should fire.
当您需要发送频繁重复的请求(例如自动完成/结果过滤)并且只有最新的处理程序应该触发时,可以采用相同的模式。