vue vue-resource網絡請求


在使用get/post 網絡請求,需要下載插件 "vue-resource"

npm install vue-resource -s

在路由要導入及注冊

import Vue from 'vue'
import Router from 'vue-router'
import VueResource from 'vue-resource'
//import Hello from '@/components/Hello'

Vue.use(Router)
Vue.use(VueResource)
// 基於全局Vue對象使用http
Vue.http.get('/someUrl', [options]).then(successCallback, errorCallback);
Vue.http.post('/someUrl', [body], [options]).then(successCallback, errorCallback);


// 在一個Vue實例內使用$http
this.$http.get('/someUrl', [options]).then(successCallback, errorCallback);
this.$http.post('/someUrl', [body], [options]).then(successCallback, errorCallback);
 
        

emulateHTTP的作用

如果Web服務器無法處理PUT, PATCH和DELETE這種REST風格的請求,你可以啟用enulateHTTP現象。啟用該選項后,請求會以普通的POST方法發出,並且HTTP頭信息的X-HTTP-Method-Override屬性會設置為實際的HTTP方法。

Vue.http.options.emulateHTTP = true;

emulateJSON的作用

如果Web服務器無法處理編碼為application/json的請求,你可以啟用emulateJSON選項。啟用該選項后,請求會以application/x-www-form-urlencoded作為MIME type,就像普通的HTML表單一樣。

Vue.http.options.emulateJSON = true;

GET請求
this.$http.get("url", {params: objArg})
.then(function(res){
// success
},function(){
//error
})

POST請求
this.$http.post('url', {params}, {emulateJSON: true})
.then(function(res){
//success
},function(){
//error
})



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM