如何在jqueryajax请求中使用timeout属性和post方法?

如何在jqueryajax请求中使用timeout属性和post方法?,jquery,ajax,timeout,Jquery,Ajax,Timeout,我有以下功能: 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':tes

我有以下功能:

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请求的响应,则会出现警告消息“您的internet连接有问题”。否则,应该执行正常函数

有人能帮忙吗


提前谢谢

您可以在
$.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 );    
});
}
  • 只需打开jquery.js文件
  • 现在查找
    jQtimeout
  • 默认设置时间为
    60000
    毫秒,替换为
    120
    秒的时间,单位为毫秒
    120000
  • var jQtimeout=120000看起来像这样
  • 完成了
  • 享受Shivesh Chandra:)

    尝试使用

    $.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
            } 
        }});
    
    您可以在以下位置获得更多信息:

    自jQuery 1.2以来,您可以通过PlainObject向提供所有参数

    因此,您的代码片段可以这样重写:

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