Vue.http.options.root = '/root';
new Vue({
http: {
root: '/root',
headers: {
Authorization: ''
}
}
})
new Vue({
ready: function() {
// get 请求
this.$http.get({url: '', headers: { Authorization: '' } })
.then(() => {
// 请求成功回调
}, () => {
// 请求失败回调
});
}
});
Vue.http(option);
this.$http(option)
new Vue({
ready: function () {
// POST请求
this.$http({
url: '',
method: 'POST',
// 请求体重发送的数据
data: {
cat: 1
},
// 设置请求头
headers: {
'Content-Type': 'x-www-from-urlencoded'
}
}).then(function () {
// 请求成功回调
}, function () {
// 请求失败回调
});
}
});
this.$http.post(
'http://example.com',
// 请求体重发送数据给服务端
{
cat: 1,
name: 'newBook'
},{
'headers': {
'Content-Type': 'x-www-form-urlencoded'
}
}).then(function () {
// 成功回调
}, function () {
// 失败回调
});
| 参数 | 类型 | 描述 |
|---|---|---|
| url | string | 请求的URL |
| method | string | 请求的HTTP方法,例如:'GET', 'POST'或其他HTTP方法 |
| body | Object,FormDatastring | request body |
| params | Object | 请求的URL参数对象 |
| headers | Object | request header |
| timeout | number | 单位为毫秒的请求超时时间 (0 表示无超时时间) |
| before | function(request) | 请求发送前的处理函数,类似于jQuery的beforeSend函数 |
| progress | function(event) | ProgressEvent回调处理函数 |
| credientials | boolean | 表示跨域请求时是否需要使用凭证 |
| emulateHTTP | boolean | 发送PUT, PATCH, DELETE请求时以HTTP |
| emulateJSON | boolean | 将request body以application/x-www-form-urlencoded content type发送 |
Vue.http({
url: 'http://example.com/{book}',
params: {
book: 'vue',
cat: 1
}
});
Vue.http({
url: 'http://example.com/book',
method: 'JSONP',
jsonp: 'cb'
});
if ( request.timeout ) {
timeout = setTimeout(function () {
reqest.cancel();
}, request.timeout);
}
// 超时后,Promise会被 reject,错误回调会被执行。
Vue.http.options.emulateHTTP = true;
Vue.http.options.emulateJSON = true;
if ( request.crossOrgin === null ) {
request.corssOrigin = corssOrigin(request);
}
if ( request.corssOrigin ) {
if ( !xhrCors ) {
request.client = xdrClient;
}
request.enumlateHTTP = false;
}
resource(url ,[params], [actions], [options]);
this.$resource('/books/{cat}', { cat: 1 });
// 解析的URL为:/books/1
Resource.actions = {
get: {method: 'GET'},
save: {method: 'POST'},
query: {method: 'GET'},
update: {method: 'PUT'},
remove: {method: 'delete'},
delete: {method: 'DELETE'}
}
this.$resource(
'/books/{cat}',
{
cat: 1
}, {
charge: {
method: 'POST',
params: {
charge: true
}
}
});
var resouce = this.$resource('/books/{id}');
// 查询
// 第一个参数为params对象,优先级高于resource发方法的params参数
resoure.get({id: 1}).then(function ( response ) {
this.$set('item', response.item);
});
// 保存
// 第二个参数为要发送的数据
resource.seve({id: 1}, {item: this.item}).then(function () {
// 请求成功回调
}, function () {
// 请求失败回调
});
resource.delete({id: 1}).then(function () {
// 请求成功回调
}, function () {
// 请求失败回调
});
Vue.http.interceptors.push({
request: function ( request ) {
// 更改请求类型为POST
request.method = 'POST';
return request;
},
response: function ( response ) {
// 修改返回数据
response.data = [{
custom: 'custom'
}];
return response;
}
});
Vue.http.interceptors.push(function () {
return {
request: function ( request ) {
return request;
},
response: function ( response ) {
return response;
}
}
});
Vue.http.interceptors.push(function ( request, next ) {
// 请求发送前的处理逻辑
next(function () {
// 请求发送后的处理逻辑
// 更具请求的状态, response参数会返回给 successCallback或errorCallback
return response
});
});
Vue.http.interceptors.push((request, next) => {
// 通过控制 组件的`v-show`值显示loading组件
loading.show = true;
next((response) => {
loading.show = false
return response
});
});
var xhrCors = 'withCredentials' in new XMLHttpRequest();
var promise - this.$http.post(
'http://example.com/book/cretae',
// 请求体中要发送给服务端数据
{
cat: '1',
name: 'newBook'
}, {
headers: {
'Content-Type': 'x-www-form-urlencoded'
}
}
);
promise.then(function ( response ) {
// 成功回调
}, function ( response ) {
// 失败回调
});
promise.catch(function ( response ) {
// 失败回调
});
promise.finally(function () {
// 执行完成或者失败回调后都会执行此逻辑。
});
// 所有回调函数的this都指向组件实例
Vue.http({
url: 'http://example.com/books',
// 参数部分,将会拼接在url之后
params: {
cat: 1
},
method: 'JSONP'
})
.then(function ( response ){
}, function () {
//error
});
http.$http({
url: 'http://example.com/books',
params: {
cat: 1
},
method: 'JSONP'
})
.then(function () {
// this 指向当前组件实例
}, function () {
});
this.$http.jsonp(
'http://www.example.com/books',
{
cat: 1
}
)
.then(function () {
}, function () {
});
Vue.http.heaers.post['Content-Type'] = 'application/x-www-form-urlencoded'
this.$http.post(
'http://example.com/books',
// 成功回调
function ( data, status, request ) {
if ( status == 200 ) {
consl.dir(data);
}
},
// 配置请求头
headres: {
'Content-Type': 'multipart/form-data'
}
);
// 实例配置的优先级高于全局配置
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有