简单封装ajax请求

85 阅读1分钟

封装ajax请求

ajax(url, params, cb) {
        $.ajax({
          type: 'post',
          url: url,
          data: params,
          dataType: "json",
          success: function (response) {
            cb(response)
          }
        });
      },

使用

this.ajax("请求url",{ 参数key:'参数值'},function(res){
        console.log(res) //回调函数
      })

当然,请求的type,async都可以封起来

ajax(type, url, params, async, cb) {
        $.ajax({
          type: type,
          url: url,
          data: params,
          dataType: "json",
          async: async,
          success: function (response) {
            cb(response)
          }
        })
      },