参见英文答案 >
Mailchimp how to call mailchimp 3.0 API in javascript 1个
我已经看到了其他几个这样的问题,但它们并没有解决问题.我使用MailChimp的API进行简单调用,在注册时将成员添加到我的邮件列表中.
我已经看到了其他几个这样的问题,但它们并没有解决问题.我使用MailChimp的API进行简单调用,在注册时将成员添加到我的邮件列表中.
但是,当我测试时,我获得了401未授权,并且API抱怨没有提交API密钥.当我在Chrome中检查请求时,我看不到任何授权标头.这是我的代码:
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控制台错了吗?
角度版本:5.2.4
解决方法
问题不在于Angular.现代浏览器为大多数跨域请求发送
preflight OPTIONS request,而Mailchimp不支持. Mailchimp API不支持客户端实现:
MailChimp does not support client-side implementation of our API using
@H_301_37@
CORS requests due to the potential security risk of exposing account
API keys.如果说得更清楚一点会很好,但我一开始并没有注意到它.最好的解决方案是使用jsonp.