vue之axios

来源:互联网 发布:带windows系统的平板 编辑:程序博客网 时间:2024/05/17 01:15

1.安装:npm, bower或cdn加载

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

axios.get('/detail', {    //参数   params: {        id: 10    } }).then(function (res) {    //成功获取数据    console.log(res); }).catch(function (err) {    //请求错误    console.log(err); });

3.post请求

//执行post请求 axios.post('/add', {    name: '前端君',    age: 26 }).then(function (res) {    //请求成功  console.log(res); }).catch(function (err) {    //请求失败  console.log(err); });


axios({    method: 'post',    url: '/user',    data: {        name: '前端君',    } });


4.多请求

function getProfile(){    //请求1    return axios.get('/profile'); } function getUser(){    //请求2    return axios.get('/user'); } //并发请求 axios.all([    getProfile(),    getUser() ]).then(axios.spread((res1, res2)=>{    //两个请求现已完成   console.log(res1);    console.log(res2); }));



原创粉丝点击