Vue.js的ajax

  1. vue-resource是vue.js的插件提供了使用XMLHttpRequest或JSOUP进行web请求和处理响应的服务。但是当vue更新到2.0后,宣布不再更新,而是推荐了axios.因此,对于其只需要了解就行

  2. Axios是一个基于promise的HTTP库

  3. 引入axios

    <script type="text/javascript" src="js/axios.js"></script>
  4. get请求

    axios.get('/user?ID=12345')
      .then(function(response){
      console.log(response);
      })
      .catch(function(err){
      console.log(err);
      })
    //也可以通过下面这种方式进行请求
    axios.get('user',{
    params:{
    ID:12345
    }
    })
    .then(function(response){
    console.log(response);
    })
    .catch(function(err){
    console.log(err);
    });

5.post请求

axios.post('user',{
firstName:'Fred',
lastName:'Flintstone'
})
.then(function(res){
console.log(res);
})
.catch(function(err){
console.log(err);
});

6.为方便起见。为所有支持的请求方法提供了别名

axios.request(config)
axios.get(url[,config])
axios.delete(url[,config])
axios.head(url[,config])
axios.post(url[,data[,config]])
axios.put(url[,data[,config]])
axios.patch(url[,data[,config]])

 

posted @ 2020-07-12 23:14  IT特工  阅读(205)  评论(0编辑  收藏  举报