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


posted on 2017-06-26 16:07  爱拼才有钱  阅读(1791)  评论(0编辑  收藏  举报

导航