vue.js axios

来源:互联网 发布:什么叫网络级防火墙 编辑:程序博客网 时间:2024/06/07 04:06
 vue更新到2.0之后,作者就宣告不再对vue-resource更新,而是推荐的axios,前一段时间用了一下,现在说一下它的基本用法。


本案例给单独引入vue.js文件的用户参考。

一、引入axios.js
<script src="../script/axios.min.js"></script>

二、例子

1、  发送一个GET请求

//通过给定的ID来发送请求 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); });
关于get请求的参数传递,你既可以直接拼接在路径中,也可以通过params来传递,交互他会帮你转为字符串拼接在路径上

2、 发送一个POST请求

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