Vue交互

vue本身不支持交互,所以在交互時需要引入模塊vue-resource.js

1.直接獲取文件內的內容:

```

'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獲取數據

```

'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)});

}

}

});

};

```

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容