axios

166 阅读1分钟

axios 文档地址:www.kancloud.cn/yunye/axios…

在全面配置中传参数 ☆要使用params 不要使用data
created(){

    /* .get 和 .post 都是简写 */
    /* 执行 GET 请求 */
    /* 第一种方式:get加参数可以在请求上直接加 */
    axios.get('https://cnodejs.org/api/v1/topics?name=zhangsan')
    .then(res=>{
      console.log(res.data)
    })
    /* 第二种方式:使用{params:{xxx:xxx}} */
    axios.get('http://timemeetyou.com:8889/api/private/v1/users',{
      params:{
        name:"zhangsan"
      }
    })
    .then(res=>{
      console.log(res.data)
    })
    .catch(err=>{
      console.log(err)
    })
    /* 在全面配置中传参数 ☆要使用params 不要使用data */
    axios({
      method: 'get',
      url: 'http://timemeetyou.com:8889/api/private/v1/users',
      headers:{
        'Authorization':'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjUwMCwicmlkIjowLCJpYXQiOjE2NDc1MDg4MDIsImV4cCI6MTY0NzU5NTIwMn0.JOGq-rUx2XFQGp0qpW8PkUyPV-O1VElOheZy2js0FJ8'
      },
      params:{
        pagenum:1,
        pagesize:10
      }
    })
    .then(res=>{
      console.log(res)
    })
    .catch(err=>{
      console.log(err)
    })
\
\
\
    https://api.shop.eduwork.cn/api/auth/login
    /* axios如何使用post请求 */
    axios.post('https://api.shop.eduwork.cn/api/auth/login',{
      email:"super@a.com",
      password:"123123"
    })
    .then(res=>{
      console.log(res)
    })
    .catch(err=>{
      console.log(err)
    })
    /* 使用全面配置的方式 发送post请求 */
    axios({
      method: 'post',
      url: 'http://timemeetyou.com:8889/api/private/v1/login',
      data: {
        username:"admin",
        password:"123456"
      }
    })
    .then(res=>{
      console.log(res)
    })
  }
}