jquery Ajax 全局调用封装实例详解

前端之家收集整理的这篇文章主要介绍了jquery Ajax 全局调用封装实例详解前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

前言:

有一种情况:全站都要用异步方式来调用 数据,提交数据,那么你每次操作 都会要$.ajax({.....})

写重复的方法代码,冗余太大, 也浪费时间,虽说你有代码自动提示补全,但真的不优雅,身为前端极客,是不能允许的!

【嘿嘿!虽说我现在基本不用jquery了 ,不过异步概念 是永远要用的,就帮助下新人】

jQuery Ajax通用js封装

第一步:引入jQuery库

第二步:开发Ajax封装类,已测试通过,可以直接调用,直接贴代码,讲解就省了

用户其它操作必须等待请求完成才可以执行。 * type 请求方式("POST" 或 "GET"), 默认为 "GET" * dataType 预期服务器返回的数据类型,常用的如:xml、html、json、text * successfn 成功回调函数 * errorfn 失败回调函数 */ jQuery.ax=function(url,data,async,type,dataType,successfn,errorfn) { async = (async==null || async=="" || typeof(async)=="undefined")? "true" : async; type = (type==null || type=="" || typeof(type)=="undefined")? "post" : type; dataType = (dataType==null || dataType=="" || typeof(dataType)=="undefined")? "json" : dataType; data = (data==null || data=="" || typeof(data)=="undefined")? {"date": new Date().getTime()} : data; $.ajax({ type: type,async: async,data: data,url: url,dataType: dataType,success: function(d){ successfn(d); },error: function(e){ errorfn(e); } }); };

/**

  • ajax封装
  • url 发送请求的地址
  • data 发送到服务器的数据,数组存储,如:{"date": new Date().getTime(),"state": 1}
  • successfn 成功回调函数
    */
    jQuery.axpost=function(url,successfn) {
    data = (data==null || data=="" || typeof(data)=="undefined")? {"date": new Date().getTime()} : data;
    $.ajax({
    type: "post",dataType: "json",success: function(d){
    successfn(d);
    }
    });
    };

/**

  • ajax封装
  • url 发送请求的地址
  • data 发送到服务器的数据,数组存储,如:{"date": new Date().getTime(),"state": 1}
  • dataType 预期服务器返回的数据类型,常用的如:xml、html、json、text
  • successfn 成功回调函数
  • errorfn 失败回调函数
    */
    jQuery.axspost=function(url,errorfn) {
    data = (data==null || data=="" || typeof(data)=="undefined")? {"date": new Date().getTime()} : data;
    $.ajax({
    type: "post",error: function(e){
    errorfn(e);
    }
    });
    };

});

第三步:调用模拟

<title>jQuery Ajax封装通用类测试</title> <@R_<a href="https://www.jb51.cc/tag/403/" target="_blank" class="keywords">403</a>_338@ http-equiv="pragma" content="no-cache"&gt; <@R_<a href="https://www.jb51.cc/tag/403/" target="_blank" class="keywords">403</a>_338@ http-equiv="cache-control" content="no-cache"&gt; <@R_<a href="https://www.jb51.cc/tag/403/" target="_blank" class="keywords">403</a>_338@ http-equiv="expires" content="0"&gt; <@R_<a href="https://www.jb51.cc/tag/403/" target="_blank" class="keywords">403</a>_338@ http-equiv="keywords" content="keyword1,keyword2,keyword3"&gt; <@R_<a href="https://www.jb51.cc/tag/403/" target="_blank" class="keywords">403</a>_338@ http-equiv="description" content="This is my page"&gt; <jsp:include page="/view/common/js_taglib.jsp"&gt;</jsp:include> <script type="text/javascript"&gt; $(function(){ $.ax( getRootPath()+"/test/ajax.html",null,function(data){ alert(data.code); },function(){ alert("出错了"); } ); $.axpost(getRootPath()+"/test/ajax.html",function(data){ alert(data.data); }); $.axspost(getRootPath()+"/test/ajax.html",function(){ alert("成功了"); },function(){ alert("出错了"); }); }); </script>

如上代码:只要填写 url,和要传输的 data 字段就行了,避免了重复工作和代码冗余。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持

原文链接:https://www.f2er.com/ajax/42450.html

猜你在找的Ajax相关文章