【笔记】防止ajax重复请求的GET和POST方法

/*
    防止ajax重复请求的GET和POST方法
*/
jQuery.extend({
    getx:
function (url,data,callback,sender){
        
var  params = {url:url,data: null ,callback: null ,sender: null };
        
for ( var  i = 1 ;i < arguments.length;i ++ ){
            
if (arguments[i] != null ){
                
var  ptype = typeof (arguments[i]);
                
if (ptype == " function " ){
                    params.callback
= arguments[i];
                }
                
else   if (ptype == " string " ){
                    params.sender
= $(arguments[i]);
                    
if (params.sender.data( " isRequesting " ))
                        
return   null ;
                }
                
else   if (ptype == " object " ){
                    
if (arguments[i].tagName  ||  arguments[i].selector){
                        params.sender
= $(arguments[i]);
                        
if (params.sender.data( " isRequesting " ))
                            
return   null ;
                    }
                    
else {
                        params.data
= arguments[i];
                    }
                }
            }
        }
        params.sender.data(
" isRequesting " , true );
        
return  $.get(params.url,params.data, function (data,status){
            params.sender.data(
" isRequesting " , false );
            
if (data == " unlogin " ){
                window.top.location.href
= " /login.aspx " ;
                
return ;
            }
            
if (params.callback){
                params.callback (data,status);
            }
        });
    },
    postx:
function (){
        
var  params = {url:url,data: null ,callback: null ,sender: null };
        
for ( var  i = 1 ;i < arguments.length;i ++ ){
            
if (arguments[i] != null ){
                
var  ptype = typeof (arguments[i]);
                
if (ptype == " function " ){
                    params.callback
= arguments[i];
                }
                
else   if (ptype == " string " ){
                    params.sender
= $(arguments[i]);
                    
if (params.sender.data( " isRequesting " ))
                        
return   null ;
                }
                
else   if (ptype == " object " ){
                    
if (arguments[i].tagName  ||  arguments[i].selector){
                        params.sender
= $(arguments[i]);
                        
if (params.sender.data( " isRequesting " ))
                            
return   null ;
                    }
                    
else {
                        params.data
= arguments[i];
                    }
                }
            }
        }
        params.sender.data(
" isRequesting " , true );
        
return  $.post(params.url,params.data, function (data,status){
            params.sender.data(
" isRequesting " , false );
            
if (data == " unlogin " ){
                window.top.location.href
= " /login.aspx " ;
                
return ;
            }
            
if (params.callback){
                params.callback (data,status);
            }
        });
    }
})

当然,这里可以抽出共同的一部分,也可以去封装$.ajax方法,但这里只是笔记...

 

 

你可能感兴趣的:(Ajax)