如何在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 01:41:17  来源:igfitidea点击:

How to use timeout attribute with post method in jQuery ajax request?

jqueryajaxtimeout

提问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 $.ajaxSetupmethod 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

  1. Just Open jquery.jsFile.
  2. Now Find jQtimeout
  3. Default Set Time is 60000Milisecond, Replace with Your Time In Milisecond 120000for 120Seconds
  4. var jQtimeout = 120000;Look like This
  5. It's Done.
  1. 只需打开jquery.js文件。
  2. 现在查找 jQtimeout
  3. 默认设置时间为60000毫秒,替换为您的毫秒时间120000120
  4. var jQtimeout = 120000;看起来像这样
  5. 完成。

Enjoy with Shivesh Chandra :)

和 Shivesh Chandra 一起享受吧 :)