vue post请求的几种方式 axios
一、axios的post请求 方式1.方式1拼接send: function () {axios.post('post.php','name=alice&num=20&').then(resp=>{console.log(resp);this.josarr=resp.data.jokes//数据赋值...
·
一、axios的post请求 方式
1.方式1拼接
send: function () {
axios.post('post.php','name=alice&num=20&').then(resp=>{
console.log(resp);
this.josarr=resp.data.jokes //数据赋值
}).catch(resp =>{
console.log('failure');
})
}
2.对于参数复杂的 先定义参数数组 传值用transformRequest函数循环凭借
js代码
new Vue({
el:'#itany',
data:{
user:{
name:'alice',
age:19
}
},
methods:{
sendPost(){
axios.post('server.php',this.user,{
transformRequest:[
function(data){
let params='';
for(let index in data){
params+=index+'='+data[index]+'&';
}
return params;
}
]
})
.then(resp => {
console.log(resp.data);
}).catch(err => {
console.log('请求失败:'+err.status+','+err.statusText);
});
},
}
});
直接$http.post需要引用vue-resource.min.js
<script src="https://cdn.staticfile.org/vue-resource/1.5.1/vue-resource.min.js"></script>
参考菜鸟https://www.runoob.com/vue2/vuejs-ajax.html
jsonp跨域 模拟360搜索
sendJSONP(){
//https://sug.so.360.cn/suggest?callback=suggest_so&encodein=utf-8&encodeout=utf-8&format=json&fields=word&word=a
this.$http.jsonp('https://sug.so.360.cn/suggest',{
params:{
word:'a'
}
}).then(resp => {
console.log(resp.data.s);
});
},
更多推荐
已为社区贡献1条内容
所有评论(0)