【发布时间】:2018-01-15 23:46:14
【问题描述】:
当我发布带有授权标头的请求 API 时,我在 Angular 4 中遇到问题?
addPost(body): Observable<any>{
const url = 'https://xxxxxx';
return this.http.post(URL, body, this.options)
.map((res:Response) => {
this.data = res.json();
})
}
token 来自 firebase 身份验证
afAuth.auth.currentUser.getIdToken()
.then(token => {
this.headers = new Headers({
'Authorization': 'Bearer '+token
});
this.headers.append('Content-Type', 'application/json');
this.options = new RequestOptions({ headers: this.headers });
});
this.headers.append('Content-Type', 'application/json');
this.options = new RequestOptions({ headers: this.headers });
我用这个打电话发帖
return this.getJob.addPost(body).subscribe((data) => {
console.log(data);
});
响应 403!!!
请求网址:https://us-central1-xxxxxx-prod.cloudfunctions.net/api/post
请求方法:OPTIONS
状态码:403
远程地址:216.58.198.51:443
推荐人政策:no-referrer-when-downgrade
响应标头
访问控制允许方法:GET、POST
访问控制允许来源:
缓存控制:私有
内容编码:gzip
内容长度:32
内容类型:文本/html;字符集=utf-8
日期:格林威治标准时间 2017 年 8 月 8 日星期二 12:23:55
etag:W/"c-dAuDFQrdjS3hezqxDTNgW7AOlYk"
函数执行 ID:d0la00v58w7p
服务器:谷歌前端
状态:403
变化:接受编码
x-cloud-trace-context:d1b6ff9d729f7e250193a70aea16cac1;o=1
x-cloud-trace-context:d1b6ff9d729f7e250193a70aea16cac1
x-powered-by:Express
请求标头
:authority:us-central1-xxxxxx-prod.cloudfunctions.net
:method:OPTIONS
:path:/api/post
:scheme:https
接受:/*
接受编码:gzip,放气,br
接受语言:en-US,en;q=0.8,ar;q=0.6
访问控制请求标头:授权、内容类型
访问控制请求方法:POST
alexatoolbar-alx_ns_ph:AlexaToolbar/alx-4.0.1
缓存控制:无缓存
来源:http://localhost:8080
编译指示:无缓存
推荐人:http://localhost:8080/extras/addjobpost
用户代理:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like
Gecko) Chrome/59.0.3071.115 Safari/537.36
【问题讨论】:
-
解决这个问题的更好方法是创建一个扩展 Angular 的 http 模块的拦截器
-
console 选项返回这个 RequestOptions {method: null, headers: Headers, body: null, url: null, params: undefined…} 是真的吗?
标签: angular http http-headers http-post