我需要通过组件 post 向 mailchimp 订阅列表发出 http 请求

I need to do a http request to a mail chimp subscription list via a component post

我需要通过组件向邮件黑猩猩订阅列表发出 http 请求 post

我已经阅读了 mail chimp 文档,但找不到任何相关内容。 我还在 angular 2 html5 视图中尝试了他们的邮件黑猩猩嵌入形式,但由于某些奇怪的原因,它不起作用。

所以我改为向订阅列表发出 http 请求,但我无法正常工作。

我正在使用 typescript,angular2 和 mail chimp

到目前为止,这是我的代码:

  subscribe = () => {
        var url = "https://mysubscriptionlist.us10.list-manage.com/subscribe/post?u=b0c935d6f51c1f7aaf1edd8ff&id=9d740459d3&subscribe=Subscribe&EMAIL=" + this.email;    
        this.jsonp.request(url).subscribe(response => {
           console.log(response);
        });   
  }

这是我当前在 chrome 中的控制台日志错误:

未捕获的语法错误:意外的标记 <

我终于找到了解决问题的方法。您需要使用 Angular2 的 Jsonp 支持。

您的地址通过向 URL 添加 c 查询参数并通过 https://mysubscriptionlist.us10.list-manage.com/subscribe/post-json 切换 https://mysubscriptionlist.us10.list-manage.com/subscribe/post 来支持 Jsonp。你需要把JSONP_CALLBACK值放进去(见本期:https://github.com/angular/angular/issues/5613)。

在这种情况下,您将拥有以下响应负载:

JSONP_CALLBACK (
{
  "result": "success",
  "msg": "Almost finished... We need to confirm your email address. To complete the subscription process, please click the link in the email we just sent you."
}
)

调用bootstrap函数时注册JSONP_PROVIDERS后:

import {bootstrap} from 'angular2/platform/browser'
import {JSONP_PROVIDERS} from 'angular2/http'
import {AppComponent} from './app.component'

bootstrap(AppComponent, [ JSONP_PROVIDERS ]);

然后您可以使用从构造函数注入的 Jsonp class 实例执行您的请求:

import {Component} from 'angular2/core';
import {Jsonp} from 'angular2/http';

@Component({
  selector: 'my-app',
  template: `
    <div>
      Result: {{result | json}}
    </div>
  `
})
export class AppComponent {
  constructor(jsonp:Jsonp) {
    var url = 'https://mysubscriptionlist.us10.list-manage.com/subscribe/post-json?u=(...)&subscribe=Subscribe&EMAIL=my@email.com&c=JSONP_CALLBACK';
    jsonp.request(url, { method: 'Get' })
     .subscribe((res) => {
       this.result = res.json()
     });
  }
}

请参阅此 plunkr 以获取工作示例:http://plnkr.co/edit/dqreqBL6kyNkR8Z2wgGR?p=preview