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)});

参数

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容