当前位置: 首页 > 工具软件 > jQuery AOP > 使用案例 >

jquery 拦截 post 等请求实现aop效果

孔琪
2023-12-01

 
   
  1. $(function(){
  2. jQuery.extend({
  3. put: function( url, data, callback, type ) {
  4. // shift arguments if data argument was omited
  5. if ( jQuery.isFunction( data ) ) {
  6. type = type || callback;
  7. callback = data;
  8. data = {};
  9. }
  10. return jQuery.ajax({
  11. type: "PUT",
  12. url: url,
  13. data: data,
  14. success: callback,
  15. dataType: type
  16. });
  17. },
  18. del: function( url, data, callback, type ) {
  19. // shift arguments if data argument was omited
  20. if ( jQuery.isFunction( data ) ) {
  21. type = type || callback;
  22. callback = data;
  23. data = {};
  24. }
  25. return jQuery.ajax({
  26. type: "DELETE",
  27. url: url,
  28. data: data,
  29. success: callback,
  30. dataType: type
  31. });
  32. }
  33. })
  34. jQuery.each( [ "get", "post","put","del"], function( i, method ) {
  35. // jQuery.get或jQuery.post为
  36. jQuery[ method ] = function( url, data, callback, type ) {
  37. if($("#loading")){
  38. $("#loading").show()
  39. }
  40. var callbackafter = function(){
  41. if($("#loading")){
  42. $("#loading").hide()
  43. }
  44. callback.call(window,arguments[0]);
  45. }
  46. // 模拟重载
  47. if ( jQuery.isFunction( data ) ) {
  48. type = type || callback;
  49. callback = data;
  50. data = undefined;
  51. }
  52. // 利用jQuery.ajax完成任务
  53. return jQuery.ajax({
  54. url: url,
  55. type: method,
  56. dataType: type,
  57. data: data,
  58. success: callbackafter
  59. });
  60. };
  61. });
  62. })()
  63. function disableAop(){
  64. jQuery.each( [ "get", "post","put","del"], function( i, method ) {
  65. // jQuery.get或jQuery.post为
  66. jQuery[ method ] = function( url, data, callback, type ) {
  67. // 模拟重载
  68. if ( jQuery.isFunction( data ) ) {
  69. type = type || callback;
  70. callback = data;
  71. data = undefined;
  72. }
  73. // 利用jQuery.ajax完成任务
  74. return jQuery.ajax({
  75. url: url,
  76. type: method,
  77. dataType: type,
  78. data: data,
  79. success: callback
  80. });
  81. };
  82. });
  83. }

拦截就是重写jquery默认的post等方法
还有一个禁用aop的就是把源码再声明一遍即可


 

 类似资料: