Axios&fetch

axios

安装

npm install axios

请求

improt Axios from 'axios'
Axios.get(url, config)
Axios.post(url, data, config)
Axios.post('/webapi/book/info', {
  bookId: 7075545,
  user_id: 8000000,
  timestamp: 1576812779109,
  sign: 'fc4a2b719a4049cdb09dc38da3686b1e',
  shuqi_h5: ''
}, {
transformRequest (data) {
          // 会自动接收到 data 参数。需要 返回,返回的内容就是请求参数的内容
          // { nodeId: 70022794, pagesize: 3, pageidx: 1 } 
          //=>nodeId=70022794&pagesize=3&pageidx=1
           // 提供一个数组
            let arr = []
            // 遍历对象
            for (let key in data) {
                arr.push(`${key}=${data[key]}`)
            }
            // 返回并 join  相当于转换成了from格式的请求
            return arr.join('&')
       }
  params: {
    _: new Date().getTime()
  }
}).then(response => {
  const { data } = response
  console.log(data)
})

参数

fetch

安装

npm install fetch

请求

improt fecth from 'fetch'
fetch("**",{
    credentials:"include",
    method:'post',
    headers: {
       "Content‐Type": "application/x‐www‐form‐urlencoded"
    },
    body: "name=kerwin&age=100",
    //上为form传参,后为jsom传参
    headers: {
        "Content‐Type": "application/json"
    },
    body: JSON.stringify({
        name:"kerin",
        age:100
    })
}).then(res=>res.json()).then(res=>{console.log(res)});

参数

你可能感兴趣的:(Axios&fetch)