vue官方之前推薦使用vue-resource套件來執行HTTP Request,不過vue更新到2.0之後,作者就宣告不再更新vue-resource,目前漸漸被axios取代。所以這篇只是做為紀錄性質,寫一下使用vue-resource的方法。
請先用指令npm安裝vue-resource
$ npm install vue-resource
先在專案中加入這個套件
# src/main.js import VueResource from 'vue-resource' Vue.use(VueResource)
請根據需求設定{URL} {BODY} {HEADER}
1. GET
this.$http({
 url: {URL},
 method: 'GET',
 body: {},
 headers: {HEADER}
 }).then(function (response) {
  console.log('success');
 }, function (response) {
  console.log('fail');
});
2. POST
this.$http.post({URL}, {BODY}, {HEADER}).then(
 function (response) {
  console.log('success');
 }, function (response) {
  console.log('fail');
});
this.$http({
 url: {URL},
 method: 'POST',
 body: {BODY},
 headers: {HEADER}
 }).then(function (response) {
  console.log('success');
 }, function (response) {
  console.log('fail');
});
 
沒有留言:
張貼留言