Vue.js+axios-npm 获取服务

首先看下官网上说的

执行GET请求

// Make a request for a user with a given ID
axios.get('/user?ID=12345')
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });
 
// Optionally the request above could also be done as
axios.get('/user', {
    params: {
      ID: 12345
    }
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

执行POST请求

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

执行多个并发请求

function getUserAccount() {
  return axios.get('/user/12345');
}
 
function getUserPermissions() {
  return axios.get('/user/12345/permissions');
}
 
axios.all([getUserAccount(), getUserPermissions()])
  .then(axios.spread(function (acct, perms) {
    // Both requests are now complete
  }));

axios API

可以通过传递相关配置来进行请求axios

// 发送POST请求
axios({
  method: 'post',
  url: '/user/12345',
  data: {
    firstName: 'Fred',
    lastName: 'Flintstone'
  }
});
axios({
  method:'get',
  url:'http://bit.ly/2mTM3nY',
  responseType:'stream'
})
  .then(function(response) {
  response.data.pipe(fs.createWriteStream('ada_lovelace.jpg'))
});

关于axios我觉官网写的很好,就简单的总结一下,我自己的理解,

首先我们将项目引入到vue项目中,

npm install axios

引入后我们到项目的package.json文件中看一下是否有

 文件中有了这个后我们到main.js中引入我们的axios,之后我们在调用的时候就可this.$axios

之后就可以在vue中引用,首先个人喜欢用data传递参数,

       getxqlist(id){
           let newLink = window.open();
             this.$axios({
                method:"post",
                url:"http://service.tianditu.gov.cn/queryServlet?mn=QueryService&id=1339&timeStamp=1536642705793&_=1536642705732",
                headers:{
                    'Content-type': 'application/x-www-form-urlencoded;charset=UTF-8'
                },
                data:{
                    mn: 'updateVisits',
                    id: id,
                },
                transformRequest: [function (data) {
                    let ret = ''
                    for (let it in data) {
                    ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                    }
                    return ret
                }],
            }).then((res)=>{
                this.form=res.data
                newLink.location.href=this.form.serviceview
                console.log(res.data,78666666);
            })
            console.log('优先走这里')
       },

具体可以看这个博客,我觉得写的非常好的地址:https://blog.csdn.net/wopelo/article/details/78783442

当我按照官网的写法给post请求服务的时候,发现传进去的值后台接收不了,他很清楚的说出啦为什么和两个解决方案

这里我简单的理解一下,这种写法可以将参数传给后台,因为axios异步请求所以我们只能在刚调用方法得当时候定义一个在新页面打开的链接

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

侧耳倾听...

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值