Angular2 post 与 mailchimp

Angular2 post with mailchimp

我的 post 在 postman 中工作,但在我的应用程序中不工作。我做错了什么?

let data = obj;
      let url = 'https://us123.api.mailchimp.com/3.0/lists/{somenumber}/members';
      let username: string = 'user';
      let password: string = 'mytokenhere';
      let headers = new Headers();
      headers.append("Authorization", "Basic " + btoa(username + ":" + password));
      headers.append("Content-Type", "application/x-www-form-urlencoded");
        return this._http.post(url, data, {headers: headers}).subscribe(
            data => this.response(data),
            error => this.response(error)
        );

我在应用程序中遇到 CORS 错误:

'XMLHttpRequest cannot load https://us123.api.mailchimp.com/3.0/lists/{{somenumber}}/members. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The response had HTTP status code 501.'

Mailchimp 不支持对其 API 的客户端调用。您需要做的是设置一个服务器,该服务器可以将浏览器的请求代理到 Mailchimp。如果 Mailchimp API 不提供 CORS 响应 headers,那么您可以在客户端做的事情并不多。

如果您创建的 API 与网站在同一个域中,那么 CORS 问题将被消除(或者您也可以通过设置适当的 headers 来解决)

请参阅身份验证下的注释:

https://developer.mailchimp.com/documentation/mailchimp/guides/get-started-with-mailchimp-api-3/

更多信息: https://www.moesif.com/blog/technical/cors/Authoritative-Guide-to-CORS-Cross-Origin-Resource-Sharing-for-REST-APIs/