如何使用jQuery减少重复的AJAX请求

时间:2014-04-29 10:10:02

标签: jquery ajax

我有一个要求,当时弹出加载两个请求正在发送。但我只想要一个请求。我尝试了以下代码。请与我分享一些想法。

 $("#product_accordion").find(".pin").click(function(){
                 $.ajax({
                    dataType:"text",
                    url:"/aaa/aaa/pin.do?action=form",
                    type:"POST",                    
                    success: function(result){                  
                        if(result.indexOf('epayPinNumber') != -1){
                            $('.overlayBorder').css({"width":610,"height":192});
                            $('.overlayBorderInner').css({"padding-top":0,"padding-left":20,"width":440});
                            displayOverlay('/aaa/aaa/pin.do?action=form','block','552','192','100%','1000','#000000',($(window).width() - $('.overlayBorder').width()) / 2,'50');
                        }else{                      
                            $('.overlayBorder').css({"width":628,"height":120});
                            $('.overlayBorderInner').css({"padding-top":0,"padding-left":20,"width":440});
                            displayOverlay('/aaa/aaa/pin.do?action=form','block','573','120','100%','1000','#000000',($(window).width() - $('.overlayBorder').width()) / 2,'50');
                        }                                                               
                    }
                });
                return false;
          });    

1 个答案:

答案 0 :(得分:0)

使用e.preventDefault()停止触发默认操作。

$("#product_accordion").find(".pin").click(function(e){
                 e.preventDefault();
                 $.ajax({
                    dataType:"text",
                    url:"/aaa/aaa/pin.do?action=form",
                    type:"POST",                    
                    success: function(result){                  
                        if(result.indexOf('epayPinNumber') != -1){
                            $('.overlayBorder').css({"width":610,"height":192});
                            $('.overlayBorderInner').css({"padding-top":0,"padding-left":20,"width":440});
                            displayOverlay('/aaa/aaa/pin.do?action=form','block','552','192','100%','1000','#000000',($(window).width()
- $('.overlayBorder').width()) / 2,'50');
                        }else{                      
                            $('.overlayBorder').css({"width":628,"height":120});
                            $('.overlayBorderInner').css({"padding-top":0,"padding-left":20,"width":440});
                            displayOverlay('/aaa/aaa/pin.do?action=form','block','573','120','100%','1000','#000000',($(window).width()
- $('.overlayBorder').width()) / 2,'50');
                        }                                                               
                    }
                });
                return false;
          });