How to make jQuery automatically retry loading () if it failed?

I am doing content replacement via jquery load () in several places on the page.

<script>
  $(document).ready(function(){
    $('#row').load('/getStudentDetails',{studentId:3});
  });
</script>

Everything works fine on my dev server. On my real server, sometimes this gives me the "request is invalid" error that I see in firebug. I think the server is becoming overloaded, or maybe some kind of caching module gets confused with a few requests. I will try to figure this issue out separately, but also just want him to repeat the same query (once) if he fails. Is there a way to use jQuery for this?

+3
source share
2 answers

, $.load. error, .

<script>
 $(document).ready(function(){
  $('#row').load('/getStudentDetails',{studentId:3}, function(response, status, xhr) {
    if(status == 'error') {
      $('#row').load('/getStudentDetails',{studentId:3});
    }
  });
 });
</script>
+1

- .

function doAjax() {
    $.ajax({
        url: '/path/to/file',
        type: 'GET',
        dataType: 'html',
        data: {studentId : 3},
        error: function(xhr, textStatus, errorThrown) {
            setTimeout('doAjax()', 5000);
        }
    });
}

, setTimeout , , .

: , . setTimeout , . , , , , .

+3

All Articles