我正在查看以下vue-resource文档,其中介绍了如何设置配置:https://github.com/vuejs/vue-resource/blob/master/docs/config.md
它表示要使用通用授权值设置标头:
Vue.http.headers.common['Authorization'] = 'Basic YXBpOnBhc3N3b3Jk';
我猜这个"基本的YXBpOnBhc3N3b3Jk"值只是一个例子,但是这个值是什么,应该用什么而不是它呢?
在同一页面上,我还看到" root":
的设置 Vue.http.options.root = '/root';
我理解这是指网络应用的网址。但是,为什么vue-resource需要我告诉它这个值呢?它用于什么?
答案 0 :(得分:5)
通过向Vue.http.headers.common
对象添加标头,您告诉vue-resource在每个请求中添加这些标头。
您还可以为每个请求添加标头:
http({
url: '/someUrl',
method: 'GET',
headers: {
Authorization: 'Basic YXBpOnBhc3N3b3Jk'
}
})
关于示例中Authorization
标头的值:它是带有用户名/密码的base64编码字符串。
window.atob('YXBpOnBhc3N3b3Jk') // => "api:password"
如果您在使用vue-resource时需要使用基本身份验证,则应提供自己的用户名/密码。
请注意,使用您的应用程序的每个人都可以查看用户名/密码。 有关基本身份验证的详细信息,请参阅https://en.wikipedia.org/wiki/Basic_access_authentication。
root-property可能是REST服务的主要端点。 而不是:
http({
url: 'http://api.domain.com/v1/someRoute'
});
您可以使用以下命令设置根端点:
Vue.http.options.root = 'http://api.domain.com/v1'
// will call http://api.domain.com/v1/someRoute
http({
url: '/someRoute'
});
答案 1 :(得分:2)
如果您想以全局方式设置标头auth,请使用接收器
Vue.http.interceptors.push(function(request, next) {
request.headers['Authorization'] = 'Basic abcd' //Base64
request.headers['Accept'] = 'application/json'
next()
});
并使用选项凭据:
Vue.http.options.credentials = true;