3

I am trying to embed a mailchimp sign up form to my angular2 application.

http://kb.mailchimp.com/lists/signup-forms/add-a-signup-form-to-your-website

I am stuck at doing a http post call to mailchimp server. I am referencing the angular2 guide here: https://angular.io/docs/ts/latest/guide/server-communication.html

This is my code in the data.service.

private mailChimpUrl = 'http://us14.list-manage.com/subscribe/post?u=xxxxxxxxxxxxxxxxxx&id=xxxxxxxxxxxxxxxxx';


 addSubscriber(): Observable<string> {
        let body = JSON.stringify( { "EMAIL" : "abcdefg@hijk.com" } );
        let headers = new Headers({'Content-Type': 'application/json'});
        let options = new RequestOptions({headers: headers});

        return this.http.post(this.mailChimpUrl, body, options)
            .map(this.extractData)
            .catch(this.handleError);
    }

I understand that browser will throw an error because of CORS. I have tried to use a chrome extension plugin to try and test if the http call is working. It throws this error.

XMLHttpRequest cannot load http://us14.list-manage.com/subscribe/post?u=xxxxxxxxxxxxxxxxxx&amp;id=xxxxxxxxxxxxxxxxx. Response for preflight has invalid HTTP status code 501

Not sure if I am doing things wrongly. Question will be is there anyway I can make it work without creating a server side call to the mailchimp server? Thanks.

slvn dev
  • 55
  • 2
  • 4

1 Answers1

3

You should be able to accomplish this with a jsonp request:

import {Component} from '@angular/core';
import {Jsonp} from '@angular/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=b0c935d6f51c1f7aaf1edd8ff&id=9d740459d3&subscribe=Subscribe&EMAIL=templth@yahoo.fr&c=JSONP_CALLBACK';
    jsonp.request(url, { method: 'Get' })
         .subscribe((res) => { this.result = res.json() });

  }
}

Working Plnkr using an older version of ng2

The request should be made somewhere else other than the constructor of a component (eg. Service), but for a quick and dirty example's sake.

Note: This is untested code converted from an example using an older version of Angular 2, but the concept should still work.

Community
  • 1
  • 1
Steve
  • 11,596
  • 7
  • 39
  • 53
  • 1
    Thanks @Steve. i found another similar question subsequently. [link](http://stackoverflow.com/questions/35180562/i-need-to-do-a-http-request-to-a-mail-chimp-subscription-list-via-a-component-po/35181085#35181085) – slvn dev Oct 14 '16 at 15:47
  • Thanks for the followup, @slvndev! I've flagged this question as a duplicate to http://stackoverflow.com/questions/35180562/i-need-to-do-a-http-request-to-a-mail-chimp-subscription-list-via-a-component-po/35181085#35181085 to link the two questions together on SO. Hopefully this will make the answer easier to find for others in the future. – Steve Oct 14 '16 at 16:04