如何在jQuery ajax请求中使用post属性的timeout属性?

时间:2022-05-04 20:16:54

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 seconds to 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请求的响应,则应显示警告消息“您的Internet连接存在某些问题”。否则正常功能应该执行。

Can anyone help on this?

任何人都可以帮忙吗?

Thanks in advance.

提前致谢。

4 个解决方案

#1


2  

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

#2


1  

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/获取更多信息。

#3


0  

Since jQuery 1.2 you are able to provide all parameters to jQuery.post via PlainObject.

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

So, your snippet can be rewritten like this:

所以,您的代码段可以像这样重写:

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

#4


-3  

  1. Just Open jquery.js File.
  2. 只需打开jquery.js文件。

  3. Now Find jQtimeout
  4. 现在找到jQtimeout

  5. Default Set Time is 60000 Milisecond, Replace with Your Time In Milisecond 120000 for 120 Seconds
  6. 默认设置时间为60000毫秒,用毫秒120000替换您的时间120秒

  7. var jQtimeout = 120000; Look like This
  8. var jQtimeout = 120000;看起来像这样

  9. It's Done.

Enjoy with Shivesh Chandra :)

享受Shivesh钱德拉:)

#1


2  

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

#2


1  

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/获取更多信息。

#3


0  

Since jQuery 1.2 you are able to provide all parameters to jQuery.post via PlainObject.

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

So, your snippet can be rewritten like this:

所以,您的代码段可以像这样重写:

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

#4


-3  

  1. Just Open jquery.js File.
  2. 只需打开jquery.js文件。

  3. Now Find jQtimeout
  4. 现在找到jQtimeout

  5. Default Set Time is 60000 Milisecond, Replace with Your Time In Milisecond 120000 for 120 Seconds
  6. 默认设置时间为60000毫秒,用毫秒120000替换您的时间120秒

  7. var jQtimeout = 120000; Look like This
  8. var jQtimeout = 120000;看起来像这样

  9. It's Done.

Enjoy with Shivesh Chandra :)

享受Shivesh钱德拉:)