且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

带有ES6的jQuery ajaxSetup

更新时间:2022-12-27 12:11:14

包装Fetch api并存储您的基本数据,并将其与您随其发送的内容合并.

Wrap the Fetch api and store your base data and merge that with whatever you send with it.

class MyFetch {
  constructor(data) {
    this.data = data
  }

  post(url, data) {
    let requestData = {
      ...this.data,
      ...data
    }
    return fetch(url, {
      method: 'POST',
      body: JSON.stringify(requestData)
    })
  }
}

let myFetch = new MyFetch({
  _token: 'helloworld'
})

myFetch.post('https://httpbin.org/post',{moreData:'more'})
  .then((res) => res.json())
  .then(json => {
    console.log('Data sent:', json.data)
  })