在已有的项目中,“封装”了ajax,这个在jQuery的基础上有封装了一下,难免有点画蛇添足的意味。所以接下来的任务就是用原生的js来实现ajax的请求。

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>mAjax</title>
    6. </head>
    7. <body>
    8. <button type="button" id="get">send get</button>
    9. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
    10. <script>
    11. $(function () {
    12. /**
    13. * 封装ajax
    14. * @param url 发送请求的地址,必填;
    15. * @param data 参数 json object{};
    16. * @param type 请求方式:GET/POST/PUT/DELETE,默认值:'GET';
    17. * @param successfn 请求成功后的回调函数;
    18. * @param errorfn 请求失败后的回调函数;
    19. * @param async 是否异步:true/false,默认值:true;
    20. */
    21. jQuery.mAjax = function (url, data, type, successfn, errorfn) {
    22. type = (type === null || type === '' || typeof type === 'undefined') ? 'GET' : type.toUpperCase();
    23. let defaults = {
    24. url: url,
    25. type: 'GET',
    26. contentType: 'text/html;charset=utf-8',
    27. data: data,
    28. async: true,
    29. cache: false,
    30. dataType: 'json',
    31. success: function (data) {
    32. successfn(data);
    33. },
    34. error: function (e) {
    35. errorfn(e);
    36. }
    37. };
    38. if (type === 'POST' || type === 'PUT') {
    39. defaults.contentType = 'application/json';
    40. }
    41. // $.ajax(defaults);
    42. if (url === 1) {
    43. successfn('asdf ')
    44. }
    45. };
    46. });
    47. let mAjax = {
    48. get: function (url, data) {
    49. // $.ajax({
    50. // url:url,
    51. // type:'GET',
    52. // contentType:'text/html;charset=utf-8',
    53. // data:data,
    54. // dataType:'json',
    55. // success:function(resData){
    56. // return resData;
    57. // },
    58. // error:function(msg){
    59. // console.log('error');
    60. // }
    61. // });
    62. if (url !== undefined) {
    63. return url;
    64. }
    65. },
    66. post: function () {
    67. alert('post');
    68. },
    69. put: function () {
    70. alert('put');
    71. },
    72. delete: function () {
    73. alert('delete');
    74. }
    75. };
    76. // ajax处理
    77. let ajaxProcess = {
    78. send: function () {
    79. $.mAjax(1, {}, '', ajaxProcess.success, ajaxProcess.error);
    80. },
    81. success: function (data) {
    82. alert(data);
    83. },
    84. error: function (e) {
    85. alert(e);
    86. }
    87. };
    88. // ajax处理
    89. let ajaxProcess1 = function () {
    90. this.send = function () {
    91. $.mAjax(1, {}, '', ajaxProcess.success, ajaxProcess.error);
    92. };
    93. this.success = function (data) {
    94. alert(data);
    95. };
    96. this.error = function (e) {
    97. alert(e);
    98. }
    99. };
    100. ajaxProcess1.prototype.get = function(){
    101. alert("ajaxProcess1.prototype.get !!");
    102. };
    103. $('#get').on('click', function () {
    104. ajaxProcess.send();
    105. new ajaxProcess1().get();
    106. });
    107. </script>
    108. </body>
    109. </html>