AJAX 封装

前端之家收集整理的这篇文章主要介绍了AJAX 封装前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. $(function(){
  2. /**
  3. * ajax封装
  4. * url 发送请求的地址
  5. * data 发送到服务器的数据,数组存储,如:{"date": new Date().getTime(),"state": 1}
  6. * async 默认值: true。默认设置下,所有请求均为异步请求。如果需要发送同步请求,请将此选项设置为 false。
  7. * 注意,同步请求将锁住浏览器,用户其它操作必须等待请求完成才可以执行。
  8. * type 请求方式("POST" 或 "GET"), 默认为 "GET"
  9. * dataType 预期服务器返回的数据类型,常用的如:xml、html、json、text
  10. * successfn 成功回调函数
  11. * errorfn 失败回调函数
  12. */
  13. jQuery.syncAjax=function(url,data,async,type,dataType,successfn,errorfn) {
  14. async = (async==null || async=="" || typeof(async)=="undefined")? "true" : async;
  15. type = (type==null || type=="" || typeof(type)=="undefined")? "post" : type;
  16. dataType = (dataType==null || dataType=="" || typeof(dataType)=="undefined")? "json" : dataType;
  17. data = (data==null || data=="" || typeof(data)=="undefined")? {"date": new Date().getTime()} : data;
  18. $.ajax({
  19. type: type,async: async,data: data,url: url,dataType: dataType,success: function(d){
  20. successfn(d);
  21. },error: function(e){
  22. errorfn(e);
  23. }
  24. });
  25. };
  26. /**
  27. * ajax封装
  28. * url 发送请求的地址
  29. * data 发送到服务器的数据,数组存储,如:{"date": new Date().getTime(),"state": 1}
  30. * successfn 成功回调函数
  31. */
  32. jQuery.jsonAjax=function(url,successfn) {
  33. data = (data==null || data=="" || typeof(data)=="undefined")? {"date": new Date().getTime()} : data;
  34. $.ajax({
  35. type: "post",dataType: "json",success: function(d){
  36. successfn(d);
  37. }
  38. });
  39. };
  40. /**
  41. * ajax封装
  42. * url 发送请求的地址
  43. * data 发送到服务器的数据,数组存储,如:{"date": new Date().getTime(),"state": 1}
  44. * dataType 预期服务器返回的数据类型,常用的如:xml、html、json、text
  45. * successfn 成功回调函数
  46. * errorfn 失败回调函数
  47. */
  48. jQuery.jsonAjax2=function(url,errorfn) {
  49. data = (data==null || data=="" || typeof(data)=="undefined")? {"date": new Date().getTime()} : data;
  50. $.ajax({
  51. type: "post",error: function(e){
  52. errorfn(e);
  53. }
  54. });
  55. };
  56. });
  1. // 自己用
  1.  
  1. function MyPost(url,postdata,onstart,onend,onsucc,onfail,sync,timeout) {
  2.   onstart && onstart();//加载中
  3.   if (!timeout) {
  4.     timeout = 10000;
  5.   }
  6.   $.ajax({
  7.     url: url,type: 'POST',data: postdata,async: !sync,//sync表示是否采用同步请求
  8.     timeout: timeout,success: function (data) {
  9.       onend && onend();//加载完成
  10.       try {
  11.         var d;
  12.         if (typeof data == 'string') {
  13.           d = $.parseJSON(data);
  14.         } else {
  15.           d = data;
  16.         }
  17.         if (d['return_code'] + '' == '0') {
  18.           if (typeof d['data'] == 'string') {
  19.             if (d['data'] != '')
  20.               d['data'] = $.parseJSON(d['data']);
  21.             else
  22.               d['data'] = {};
  23.           }
  24.           onsucc && onsucc(d['data'],d['return_message']);
  25.         } else {
  26.           onfail && onfail(d['data'],d['return_message'],d['return_code']);
  27.         }
  28.       }
  29.       catch (e) {
  30.         console && console.log(e);
  31.         onfail && onfail();
  32.       }
  33.     },error: function (msg,status) {
  34.       onend && onend();
  35.       onfail && onfail();
  36.     }
  37.   });
  38. }
  39.  
  40.  
  41.  

猜你在找的Ajax相关文章