您的位置:首页 > 新闻动态 > 技术文章
2019-04-09

[vue]使用axios作http请求 类似ajax

首先导入axios

npm

npm install axios

 

直接插入js

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

或者下载到本地,随你喜欢

 

执行http请求

get请求

// 为给定 userid 创建请求

axios.get('/member.php?userid=100')

  .then(function (response) {

    console.log(response);

  })

  .catch(function (error) {

    console.log(error);

  });

 

// 可选地,上面的请求可以这样做

axios.get('/member.php', {

    params: {

      userid: 100

    }

  })

  .then(function (response) {

    console.log(response);

  })

  .catch(function (error) {

    console.log(error);

  });

 

post请求

axios.post('/memberajaxurl', {

username:username,action:"register_ajax"

  }) .then(function (response) {

    console.log(response);

  })

  .catch(function (error) {

    console.log(error);

  });

 

通过向 axios 传递相关配置来创建请求

axios({

  method: 'post',

  url: '/memberajaxurl',

  data: {

  username:username,action:"register_ajax"

  }

}).then(response=> {

this.username_notice=response.data;

}).catch(error=> {

this.username_notice=error;

})

 

*如果在Vue中使用无法将返回值指向this.data时,请使用箭头函数代替function。

axios.post('/memberajaxurl', {

username:username,action:"register_ajax"

  }) .then( response=> {

    this.response=response;

  })

  .catch(error=>{

    this.response=error;

  });

 


下一篇:[vue]使用axios作http请求 post传参问题解决[真是一个坑]
上一篇:webpack-dev-server搭建一个便捷的前端开发环境

© 2010-2020 Beasure本硕科技. All Right Reserved. 备案号:粤ICP备14074318号