npm i vue-resource --save-dev
import Vueresource from 'vue-resource'
Vue.use(Vueresource)
// 便捷方法 this.$http.get({url:'api/index', headers:{Authorization:'Basic Yxsdlfjui'}}) .them((data) => { // 请求成功 }, (error) => { // 请求失败 }) // 底层方法 Vue.http({ url:'api/index2', method: 'POST', data: { param: 1 } }) .then((data) => { // 请求成功 }, (error) => { // 请求失败 })
便捷方法是对底层方法的封装vue
this.$http.get(url,{data},{opation})
npm
var promise = this.$http.post('api/index'); promise.this(function(response){ // 成功回调 }, function(error){ // 失败回调 }) promise.catch(function(error){ // 失败回调 }) promise.finally(function(){ // 失败或者成功后都会执行此函数 }) // 全部回调函数的`this`都指向组件实例
methos
的值为JSONP
便可this.$http.jsonp()
也能够默认状况下, 发送给服务器请求头的Content-Type
为application/json
json
有时咱们须要将数据提交为指定的类型api
1 . 全局headers配置promise
Vue.http.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'
2 . 实例配置服务器
this.$http.post( 'api/index', // 成功回调 function(data, status, request) { if(status === 200) { // 成功 } }, // 实例配置 { headers: { 'Content-Type':'multipart/form-data' } } )
实力配置优先于全局配置app