vue与后台交互数据(vue-resource)
需要引入库:vue-resource<script src="https://cdn.jsdelivr.net/vue.resource/1.0.3/vue-resource.min.js"></script>1.获取普通文本数据 <!DOCTYPE html><html lang
·
需要引入库:vue-resource
<script src="https://cdn.jsdelivr.net/vue.resource/1.0.3/vue-resource.min.js"></script>
1.获取普通文本数据
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>测试 vue-resource</title>
<script src="http://unpkg.com/vue/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/vue.resource/1.0.3/vue-resource.min.js"></script>
<script type="text/javascript">
window.onload = function(){
var vm = new Vue({
el:'#box',
data:{
msg:'Hello World!',
},
methods:{
get:function(){
//发送get请求
this.$http.get('test.txt').then(function(res){
alert(res.body);
},function(){
console.log('请求失败处理');
});
}
}
});
}
</script>
</head>
<body>
<div id="box">
<input type="button" @click="get()" value="按钮">
</div>
</body>
</html>
2.get发送数据给后端
this.$http.get('http://127.0.0.1:8080/VueStudy/test/getTest.action',{a:1,b:2}).then(function(res){
alert(res.body);
},function(res){
console.log(res.status);
});
3.post发送数据到后台
//post方法
/* 如果Web服务器无法处理编码为application/json的请求,你可以启用emulateJSON选项。
启用该选项后,请求会以application/x-www-form-urlencoded作为MIME type,就像普通的HTML表单一样。*/
this.$http.post('http://127.0.0.1:8080/VueStudy/test/getTest.action',{a:1,b:2},{emulateJSON:true}).then(function(res){
alert(res.body);
},function(res){
console.log(res.status);
});
4.jsonp跨域
this.$http.jsonp('https://sug.so.360.cn/suggest',{word:'a'},{jsonp:'callback'}).then(function(res){
console.log(res.data);
},function(res){
console.log(res.status);
});
5.非简写
this.$http({
url:'http://127.0.0.1:8080/VueStudy/test/getTest.action',
method:'GET',
/!* emulateJSON:true,*!/
data:{'a':'gaoguo','b':'ceshiceshi','c':'123456','isTop':0},
/!*headers: {"X-Requested-With": "XMLHttpRequest"},*!/
emulateJSON: true
}).then(function(data){
//赋值给alllist数组,
//this.$set('alllist',data.data.knowledgeList)
})
更多推荐
已为社区贡献2条内容
所有评论(0)