**  promise+axios:

/**
* 封装get方法
* @param url
* @param data
* @returns {Promise}
*/

export function fetch(url,params={}){
  return new Promise((resolve,reject) => {
    axios.get(url,{
      params:params
    }).then(response => {
      resolve(response.data);
    }).catch(err => {
      reject(err)
    })
  })
}


/**
* 封装post请求
* @param url
* @param data
* @returns {Promise}
*/

export function post(url,data = {}){
  return new Promise((resolve,reject) => {
    axios.post(url,data).then(response => {
      resolve(response.data);
    },err => {
      reject(err)
    })
  })
}

/**
* 封装patch请求
* @param url
* @param data
* @returns {Promise}
*/

export function patch(url,data = {}){
  return new Promise((resolve,reject) => {
    axios.patch(url,data).then(response => {
      resolve(response.data);
    },err => {
      reject(err)
    })
  })
}

/**
* 封装put请求
* @param url
* @param data
* @returns {Promise}
*/

export function put(url,data = {}){
  return new Promise((resolve,reject) => {
    axios.put(url,data).then(response => {
      resolve(response.data);
    },err => {
      reject(err)
    })
  })
}

 

**  axios的get请求:

axios.get("url请求路径").then(res => {

  console.log(res);

});

 

**  axios的post请求:

axios.post('url请求路径',参数).then(res => {

  console.log(res.data);

});

 

**  fetch的get请求:

fetch("url请求路径").then(res => {

  return res;

});

 

**  fetch的post请求:

fetch("url请求路径",{

method: 'POST',

body: JSON.stringify("请求数据"),

headers: { 'Content-Type' : 'application/json'  }

}).then(res => {

  return res;

});

 

**  Jquery-ajax:

$.ajax({

  url:"请求路径",

  success:function(result){

     console.log(result)

   }

});

 

$.ajax({

  type: "post",

  dataType: "html", //数据类型,html,xml,json

  url: '请求路径',

  data: '提交参数',

  success:function(result){

    console.log(result)

   },

  error: function(err){

    console.log(err)

  }

});

 

$.get("请求路径",请求数据, function(result){ //返回结果处理 });

$.post("请求路径",请求数据, function (data) { if (data == "ok") { alert("添加成功!"); } });

$.getJSON("请求路径",请求数据function (data,status,xhr) { if (data == "ok") { alert("添加成功!"); } })

 

  **  js - ajax:

var Ajax={
  get: function(url, fn) {
    // XMLHttpRequest对象用于在后台与服务器交换数据   
    var xhr = new XMLHttpRequest();            
    xhr.open('GET', url, true);
    xhr.onreadystatechange = function() {
      // readyState == 4说明请求已完成
      if (xhr.readyState == 4 && xhr.status == 200 || xhr.status == 304) { 
        // 从服务器获得数据 
        fn.call(this, xhr.responseText);  
      }
    };
    xhr.send();
  },
  // datat应为'a=a1&b=b1'这种字符串格式,在jq里如果data为对象会自动将对象转成这种字符串格式
  post: function (url, data, fn) {
    var xhr = new XMLHttpRequest();
    xhr.open("POST", url, true);
    // 添加http头,发送信息至服务器时内容编码类型
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");  
    xhr.onreadystatechange = function() {
      if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 304)) {
        fn.call(this, xhr.responseText);
      }
    };
    xhr.send(data);
  }
}

原生js实现ajax代码来源:
https://www.jianshu.com/p/ea064da40e25;
https://www.cnblogs.com/lcy-123/p/10757088.html

 

posted on 2020-04-27 12:21  羽丫头不乖  阅读(243)  评论(0)    收藏  举报