如果vue-resource有像jQuery.ajaxSetup()这样的选项;

时间:2015-08-18 10:18:54

标签: jquery ajax vue.js

https://github.com/vuejs/vue-resource

我想将全局设置添加到Vue.http

喜欢这个

$.ajaxSetup({
    complete: function(xhr, status) {
        if (xhr.status == '401') {
            delCookie('un');
            window.location.hash = '#/login';
        }
    }
});

3 个答案:

答案 0 :(得分:0)

您似乎应该能够在Vue.http.optionsVue.http.headers中访问和设置默认选项。这似乎分别是它们的默认值:

{ // options
    method: 'get',
    params: {},
    data: '',
    xhr: null,
    jsonp: 'callback',
    beforeSend: null,
    crossOrigin: null,
    emulateHTTP: false,
    emulateJSON: false
}

var jsonType = {'Content-Type': 'application/json;charset=utf-8'};
{ // headers
    put: jsonType,
    post: jsonType,
    patch: jsonType,
    delete: jsonType,
    common: {'Accept': 'application/json, text/plain, */*'},
    custom: {'X-Requested-With': 'XMLHttpRequest'}
}

答案 1 :(得分:0)

试试这个:

new Vue({
    http: {
        root: '/root',
        headers: {
            Authorization: ''
        }
    }
})

答案 2 :(得分:0)

您需要使用拦截器 https://github.com/pagekit/vue-resource/blob/develop/docs/http.md

示例,在您的app.js或main.js中

indexOf