【问题标题】:How to use timeout attribute with post method in jQuery ajax request?如何在 jQuery ajax 请求中使用带有 post 方法的超时属性?
【发布时间】:2014-03-13 08:15:43
【问题描述】:

我有以下功能:

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 );    
    });
}

我想为这个函数分配 30 秒 的超时时间。因此,如果在 30 秒内未收到对 ajax 请求的响应,则应显示“您的互联网连接有问题”的警报消息。否则正常功能应该得到执行。

有人可以帮忙吗?

提前致谢。

【问题讨论】:

    标签: jquery ajax timeout


    【解决方案1】:

    您可以像这样在$.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 );    
    });
    }
    

    【讨论】:

      【解决方案2】:
      1. 只需打开 jquery.js 文件。
      2. 现在找到jQtimeout
      3. 默认设置时间为 60000 毫秒,用您的毫秒时间 120000 替换为 120
      4. var jQtimeout = 120000; 看起来像这样
      5. 完成了。

      与 Shivesh Chandra 一起享受 :)

      【讨论】:

        【解决方案3】:

        尝试使用

        $.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
                } 
            }});
        

        您可以在以下位置获得更多信息: http://api.jquery.com/jQuery.ajax/

        【讨论】:

          【解决方案4】:

          从 jQuery 1.2 开始,您可以通过 PlainObject 向 jQuery.post 提供所有参数。

          所以,你的 sn-p 可以这样重写:

          $.post({ url: module_url, data: { … })
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-04-30
            • 1970-01-01
            • 1970-01-01
            • 2016-10-14
            • 1970-01-01
            • 2014-08-20
            相关资源
            最近更新 更多