【发布时间】:2018-08-28 06:18:22
【问题描述】:
我已经看到了其他几个类似的问题,但它们并没有解决问题。我使用 MailChimp 的 API 进行了一个简单的调用,以便在他们注册时将成员添加到我的邮件列表中。
但是,当我测试时,我收到了 401 未授权,并且 API 抱怨没有提交 API 密钥。当我在 Chrome 中检查请求时,我没有看到任何 Authorization 标头。这是我的代码:
const formData = {
email_address: this.emailControl.value,
status: 'subscribed',
merge_fields: {
NAME: this.nameControl.value
}
};
const header = new HttpHeaders({
'Authorization': 'apikey:' + environment.mailChimpApiKey
});
this.http.post(this.mailChimpUrl, formData, {
headers: header,
observe: 'response'
}).subscribe(response => {
console.log('response', response);
if (response.status === 200) {
this.submitted = true;
}
});
我已经检查并再次检查了 HttpClient.post 方法签名,以及 MailChimp API 期望如何接收 Auth 标头。看起来我做的一切都是正确的,那么为什么 Angular 不设置标题?
我注意到,当我设置可选标头时,该值仅更改为 Access-Control-Request-Headers。我是不是读错了 chrome 控制台?
Angular 版本:5.2.4
【问题讨论】: