简体   繁体   English

带有 mailchimp 的 Angular2 帖子

[英]Angular2 post with mailchimp

My post works in postman but doesn't work inside my app.我的帖子在邮递员中有效,但在我的应用程序中无效。 What am I doing wrong?我究竟做错了什么?

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)
        );

I'm getting a CORS error in app:我在应用程序中收到 CORS 错误:

'XMLHttpRequest cannot load https://us123.api.mailchimp.com/3.0/lists/ {{somenumber}}/members. 'XMLHttpRequest 无法加载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.对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。 Origin ' http://localhost:4200 ' is therefore not allowed access.因此,不允许访问 Origin ' http://localhost:4200 '。 The response had HTTP status code 501.'响应的 HTTP 状态代码为 501。

Mailchimp doesn't support client side calls to their API. Mailchimp 不支持客户端调用其 API。 What you would need to do is setup a server that can proxy the requests from the browser to Mailchimp.您需要做的是设置一个服务器,该服务器可以将来自浏览器的请求代理到 Mailchimp。 There isn't much you can do client side to get it to work if the Mailchimp API doesn't provide the CORS response headers.如果 Mailchimp API 不提供 CORS 响应标头,则客户端无法执行太多操作以使其正常工作。

If your API that you create is on the same domain as the website, then the CORS issue would be eliminated (or you can also fix by setting the appropriate headers)如果您创建的 API 与网站在同一个域中,那么 CORS 问题将被消除(或者您也可以通过设置适当的标头来修复)

See the note under Authentication:请参阅身份验证下的注释:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM