阅读此文,默认能够使用get和post方式获取后台数据
1.传入的地址例如:"http://localhost:8000/user?id=XXX"
// 为给定 ID 的 user 创建请求
axios.get('/user?id=12345')
.then(function (response) {
console.log(response);
})
2.传入的地址例如:"http://localhost:8000/user?myUrl=http://apis.juhe.cn/mobile/get?phone=15938335231&key=6033c85baa0ef963cb62430c1b23f171" //手机号查询接口
无法通过方法一的req.query获取到手机号查询地址,解决方案:一方面可以通过后台拼接实现,如url=req.query+"&key=6033c85baa0ef963cb62430c1b23f171",然后根据地址获取数据。另一方面可以通过以下方法
axios.get('http://localhost:9000',{params:{
myUrl : `http://apis.juhe.cn/mobile/get?phone=15938335231&key=6033c85baa0ef963cb62430c1b23f171` //接口地址
}} ).then(res=>{
console.log(res);
});
此方法可以通过req.query.myUrl在服务端获取完整的接口地址,然后根据地址获取数据。