Sin descripción

util.js 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /**
  2. * ajax的二次封装
  3. * Created by huantt on 2018/7/16.
  4. * https://blog.csdn.net/u010791662
  5. */
  6. (function () {
  7. /**
  8. *
  9. * @param opts
  10. */
  11. function doAjax(opts) {
  12. this.url = opts.url;
  13. this.data = opts.data || {};
  14. // this.isShowLoad = opts.isShowLoad == undefined ? false : opts.isShowLoad;
  15. this.Type = opts.Type || "POST";
  16. this.dataType = opts.dataType || "json";
  17. this.callBack = opts.callBack;
  18. this.async = opts.async || true;
  19. this.contentType = opts.contentType == undefined ? "application/x-www-form-urlencoded" : opts.contentType;
  20. this.processData = opts.processData == undefined ? true : opts.processData;
  21. this.error = opts.error || requestError;
  22. this.init();
  23. }
  24. //初始化
  25. doAjax.prototype.init = function () {
  26. this.getRequire();
  27. };
  28. //发送请求
  29. doAjax.prototype.getRequire = function () {
  30. var self = this;
  31. // if (self.isShowLoad)
  32. // showWait(0);
  33. $.ajax({
  34. url: self.url,
  35. data: self.data,
  36. type: self.Type,
  37. dataType: self.dataType,
  38. async: self.async,
  39. contentType: self.contentType,
  40. processData: self.processData,
  41. success: function (res) {
  42. beforeResponse(res);
  43. if (self.callBack) {
  44. if ($.isFunction(self.callBack)) {
  45. self.callBack(res);
  46. } else {
  47. console.log("callBack is not a function");
  48. }
  49. }
  50. },
  51. error: self.error
  52. }
  53. );
  54. };
  55. window.doAjax = doAjax;
  56. return this;
  57. })();
  58. /**
  59. * 请求错误的统一处理
  60. * @param jqXHR
  61. * @param textStatus
  62. * @param errorThrown
  63. */
  64. function requestError(jqXHR, textStatus, errorThrown) {
  65. /*弹出jqXHR对象的信息*/
  66. // console.log(jqXHR.responseText);
  67. console.log(jqXHR.status);
  68. // console.log(jqXHR.readyState);
  69. // console.log(jqXHR.statusText);
  70. // /*弹出其他两个参数的信息*/
  71. // console.log(textStatus);
  72. // console.log(errorThrown);
  73. }
  74. /**
  75. * 执行回调函数前的统一处理
  76. * @param result
  77. */
  78. function beforeResponse(result) {
  79. }