fetch 封装

fetch.js

var http = {
  get: function (url) {
    return new Promise((resolve, reject) => {
      fetch(url)
        .then(res => res.json())
        .then(data => resolve(data))
        .catch(err => reject(err))

    })
  },

  post: function (url, data) {
    return new Promise((resolve, reject) => {
      fetch(url, {
          method: 'POST',
          headers: {
            'Content-type': 'application/json'
          },
          body: JSON.stringify(data)
        })
        .then(res => res.json())
        .then(json => resolve(JSON.parse(json.d)))
        .catch(err => reject(err))
    })
  },

  put: function (url, data) {
    return new Promise((resolve, reject) => {
      fetch(url, {
          method: 'PUT',
          headers: {
            'Content-type': 'application/json'
          },
          body: JSON.stringify(data)
        })
        .then(res => res.json())
        .then(data => resolve(data))
        .catch(err => reject(err))

    })
  },

  delete: function (url, data) {
    return new Promise((resolve, reject) => {
      fetch(url, {
          method: 'DELETE',
          headers: {
            'Content-type': 'application/json'
          },
          body: JSON.stringify(data)
        })
        .then(res => res.json())
        .then(data => resolve('删除成功'))
        .catch(err => reject(err))

    })
  }
}

调用方式:

var url = 'http://xx.asmx/xx';

var param = {
  xx: xx,
  xx: xx,
};
http.post(url, param)
      .then(data => console.log(data.msgs))
      .catch(err => console.log(err))
      //.catch(err => console.log(请求失败))

https://blog.csdn.net/qq_40190624/article/details/82873640
https://developer.mozilla.org/zh-CN/docs/Web/API/Fetch_API/Using_Fetch

posted @ 2019-06-12 22:16  Mouer404  阅读(334)  评论(0编辑  收藏  举报