如果失败,如何使jQuery自动重试load()? [英] How to make jQuery automatically retry a load() if it fails?

查看:81
本文介绍了如果失败,如何使jQuery自动重试load()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在页面上的许多地方通过jquery load()进行一些内容替换。

I'm doing some content replacement via jquery load() on a number of places on page.

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

在我的开发服务器上,一切都很完美。在我的实时服务器上,它有时会给我一个请求无效错误,我可以在firebug中看到。我认为服务器不堪重负,或者某些缓存模块可能会与多个请求混淆。我将尝试单独找出该问题,但也只是想让它在失败时重试相同的请求(一次)。是否有一个灵巧的jQuery方法呢?

On my dev server things work perfectly. On my live server sometimes it gives me a "request is invalid" error which I can see in firebug. I think the server is getting overwhelmed, or perhaps some caching module is getting confused with several requests. I'll try to figure out that issue separately, but also simply want it to retry the same request (once) if it fails. Is there a slick jQuery way to do that?

推荐答案

我不会称之为光滑,但你可以使用回调函数 $。load 。如果状态为错误,则可以重做加载。

I wouldn't call it slick, but you could use the callback function of $.load. If the status is error, you can redo the load.

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

这篇关于如果失败,如何使jQuery自动重试load()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆