jQuery版AJAX简易封装代码
2016-09-14 14:01
696 查看
开发过程中,AJAX的应用应该说非常频繁,当然,jQuery的AJAX函数已经非常好用,但是小编还是稍微整理下,方便不同需求下,可以简化输入参数,下面是实例代码:
$(function(){ /** * ajax封装 * url 发送请求的地址 * data 发送到服务器的数据,数组存储,如:{"date": new Date().getTime(), "state": 1} * async 默认值: true。默认设置下,所有请求均为异步请求。如果需要发送同步请求,请将此选项设置为 false。 * 注意,同步请求将锁住浏览器,用户其它操作必须等待请求完成才可以执行。 * type 请求方式("POST" 或 "GET"), 默认为 "GET" * dataType 预期服务器返回的数据类型,常用的如:xml、html、json、text * successfn 成功回调函数 * errorfn 失败回调函数 */ jQuery.syncAjax=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.jsonAjax=function(url, data, successfn) { data = (data==null || data=="" || typeof(data)=="undefined")? {"date": new Date().getTime()} : data; $.ajax({ type: "post", data: data, url: url, dataType: "json", success: function(d){ successfn(d); } }); }; /** * ajax封装 * url 发送请求的地址 * data 发送到服务器的数据,数组存储,如:{"date": new Date().getTime(), "state": 1} * dataType 预期服务器返回的数据类型,常用的如:xml、html、json、text * successfn 成功回调函数 * errorfn 失败回调函数 */ jQuery.jsonAjax2=function(url, data, successfn, errorfn) { data = (data==null || data=="" || typeof(data)=="undefined")? {"date": new Date().getTime()} : data; $.ajax({ type: "post", data: data, url: url, dataType: "json", success: function(d){ successfn(d); }, error: function(e){ errorfn(e); } }); }; });
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
相关文章推荐
- jQuery版AJAX简易封装
- jQuery封装的Ajax方法
- JavaScript 封装Ajax传递的数据代码
- jquery 全局AJAX事件使用代码
- jquery实现读取页面load get post ajax的几种代码方法
- jquery+ajax每秒向后台发送请求数据然后返回页面的代码
- jQuery滚屏代码,还有一点地方封装不进去
- jQuery封装方法ajax调用获得返回json数据方法
- jQuery.ajax 用户登录验证代码
- asp.net下使用jquery 的ajax+WebService+json 实现无刷新取后台值的实现代码
- 基于jQuery实现的Ajax 验证用户名是否存在的实现代码
- 使用jQuery的ajax功能实现的RSS Reader 代码
- AjaxPro2.0+jQuery+C#实现文章系统中的顶一下、踩一下的代码开发
- Jquery Ajax.ashx 高效分页实现代码
- Jquery ajaxsubmit上传图片实现代码
- jQuery对AJAX的封装
- JQuery与Ajax常用代码实现对比
- jquery Json Ajax搜索与分页效果代码
- jQuery实战中文版 第八章 Ajax代码8-3问题
- jQuery结合Ajax实现用户名是否存在的验证代码