如何在 jQuery ajax 请求中使用带有 post 方法的超时属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21771399/
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 use timeout attribute with post method in jQuery ajax request?
提问by PHPLover
I've a following function:
我有以下功能:
function mark_unmark_user_answer(targ, answer, answer_id, test_id, test_type, question_no, module_url) {
if(checked==targ){
targ.checked=false;
checked=false;
} else {
checked=targ;
}
$.post(module_url, {'test_id':test_id, 'question_no':question_no, 'op':'mark_ans', 'test_type':test_type, 'answer_no':answer, 'answer_id':answer_id}, function(data) {
if(jQuery.trim(data)=='unmark_ans') {
$('input[type="radio"]').removeAttr('checked');
$('#display_'+question_no).removeClass('green');
$('#display_'+question_no).removeClass('blue');
$('#display_'+question_no).addClass('orange');
} else {
//$('#mark_review').val('Mark');
$('#display_'+question_no).removeClass('orange');
$('#display_'+question_no).removeClass('blue');
$('#display_'+question_no).addClass("green");
$('#mark_review').attr('disabled', false);
}
var total_questions = $('#total_questions').val();
test_question_attempted_count( total_questions );
});
}
I want to assign time-out of 30 secondsto this function. So if the response for the ajax request is not received within 30 seconds then the alert message saying that "Your internet connection has some problem" should appear. Otherwise normal function should get execute.
我想为此函数指定30 秒的超时时间。因此,如果在 30 秒内未收到对 ajax 请求的响应,则应显示“您的互联网连接有问题”的警报消息。否则应该执行正常的功能。
Can anyone help on this?
任何人都可以帮忙吗?
Thanks in advance.
提前致谢。
回答by Santos L. Victor
Try use
尝试使用
$.ajax({
type: "POST",
url: your_url_request,
data: {field: value, field_2: value_2},
timeout: 1000,
error: function(jqXHR, textStatus, errorThrown) {
if(textStatus==="timeout") {
//do something on timeout
}
}});
You can has more information in: http://api.jquery.com/jQuery.ajax/
您可以在以下位置获得更多信息:http: //api.jquery.com/jQuery.ajax/
回答by DhruvJoshi
You can set defaults for Ajax request in $.ajaxSetup
method like this
您可以在这样的$.ajaxSetup
方法中为 Ajax 请求设置默认值
function mark_unmark_user_answer(targ, answer, answer_id, test_id, test_type, question_no, module_url) {
if(checked==targ){
targ.checked=false;
checked=false;
} else {
checked=targ;
}
$.ajaxSetup({
type: 'POST',
timeout: 30000,
error: function(xhr) {
$('#display_error')
.html('Error: ' + xhr.status + ' ' + xhr.statusText);
}
})
$.post(module_url, {'test_id':test_id, 'question_no':question_no, 'op':'mark_ans', 'test_type':test_type, 'answer_no':answer, 'answer_id':answer_id}, function(data) {
if(jQuery.trim(data)=='unmark_ans') {
$('input[type="radio"]').removeAttr('checked');
$('#display_'+question_no).removeClass('green');
$('#display_'+question_no).removeClass('blue');
$('#display_'+question_no).addClass('orange');
} else {
//$('#mark_review').val('Mark');
$('#display_'+question_no).removeClass('orange');
$('#display_'+question_no).removeClass('blue');
$('#display_'+question_no).addClass("green");
$('#mark_review').attr('disabled', false);
}
var total_questions = $('#total_questions').val();
test_question_attempted_count( total_questions );
});
}
回答by Gregory Igelmund
Since jQuery 1.2 you are able to provide all parameters to jQuery.postvia PlainObject.
从 jQuery 1.2 开始,您可以通过 PlainObject向jQuery.post提供所有参数。
So, your snippet can be rewritten like this:
因此,您的代码段可以像这样重写:
$.post({ url: module_url, data: { … })
回答by Shivesh96
- Just Open jquery.jsFile.
- Now Find
jQtimeout
- Default Set Time is
60000
Milisecond, Replace with Your Time In Milisecond120000
for120
Seconds var jQtimeout = 120000;
Look like This- It's Done.
- 只需打开jquery.js文件。
- 现在查找
jQtimeout
- 默认设置时间为
60000
毫秒,替换为您的毫秒时间120000
为120
秒 var jQtimeout = 120000;
看起来像这样- 完成。
Enjoy with Shivesh Chandra :)
和 Shivesh Chandra 一起享受吧 :)