vue本身不支持交互荆针,所以在交互時需要引入模塊vue-resource.js
1.直接獲取文件內(nèi)的內(nèi)容:
```
'use strict';
window.onload=function(){
new Vue({
el:'body',
data:{
a:12
},
methods:{
get:function(){
this.$http.get('a.txt').then(function(res){
console.log(res.data);
},function(err){alert(err)});
}
}
});
};
```
2.get通過get.php'
```
use strict';
window.onload=function(){
new Vue({
el:'body',
data:{
d:12
},
methods:{
get:function(){
this.$http.get('get.php',{'a':1,'b':4}).then(function(res){
console.log(res.data);
},function(err){console.log(err.status)});
}
}
});
};
```
3.post通過post.php'
```
use strict';
window.onload=function(){
new Vue({
el:'body',
data:{
a:1
},
methods:{
post:function(){
this.$http.post('post.php',{a:1,b:2},{emulateJSON:true}).then(function(res){alert(res.data)},function(err){console.log(err.data)});
}
}
});
```
};4.通過jsonp獲取數(shù)據(jù)
```
'use strict';
window.onload=function(){
new Vue({
el:'body',
data:{
d:12
},
methods:{
get:function(){
this.$http.jsonp('https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?',{wd:4},{jsonp:'cb'}).then(function(res){
alert(res.data.s);
},function(err){console.log(err.status)});
}
}
});
};
```