将Access-Control-Allow-Origin设置为*的Angular 2 http请求

Ang*_*arM 16 angular-http angular

我正在使用angular2和打字稿.

我正试图发布到我的邮件黑猩猩订阅列表.

我的代码到目前为止:

 constructor(router: Router, http: Http){   
      this.router = router;

      this.http = http; 
      this.headers = new Headers();
      this.headers.append('Content-Type', 'application/json');
      this.headers.append('Access-Control-Allow-Origin', '*');
  }

  subscribe = () => {
        var url = "https://thepoolcover.us10.list-manage.com/subscribe/post?u=b0c935d6f51c1f7aaf1edd8ff&id=9d740459d3&subscribe=Subscribe&EMAIL=" + this.email;
        this.isSuccess = false;

        this.http.request(url, this.headers).subscribe(response => {
           console.log(response);
           this.isSuccess = true; 
        });   
  }
Run Code Online (Sandbox Code Playgroud)

这是我在控制台中遇到的错误:

在此输入图像描述

我现在收到此错误:Uncaught SyntaxError:Unexpected token <

目前的代码如下:

export class Footer{
  email: string = "";
  router : Router;
  http : Http;
  jsonp: Jsonp;
  isSuccess: boolean = false;

  constructor(router: Router, jsonp: Jsonp, http: Http){   
      this.router = router;
      this.http = http; 
      this.jsonp = jsonp;
  }

  subscribe = () => {
        var url = "https://thepoolcover.us10.list-manage.com/subscribe/post?u=b0c935d6f51c1f7aaf1edd8ff&id=9d740459d3&subscribe=Subscribe&EMAIL=" + this.email;
        this.isSuccess = false;

        this.jsonp.request(url).subscribe(response => {
           console.log(response);
           this.isSuccess = true; 
        });   
  }
Run Code Online (Sandbox Code Playgroud)

Thi*_*ier 20

所述Access-Control-Allow-Origin报头是应该存在于反应,而不是在请求中.

如果您的服务支持CORS,则必须在响应头中将其返回以允许请求.所以这不是你的Angular应用程序的问题,但它必须在服务器端级别处理......

如果您需要更多详细信息,可以查看以下链接:

编辑

它似乎thepoolcover.us10.list-manage.com不支持CORS而是JSONP.您可以尝试重构您的代码,如下所述:

constructor(private router: Router, private jsonp: Jsonp){   
}

subscribe = () => {
  var url = "https://thepoolcover.us10.list-manage.com/subscribe/post?u=b0c935d6f51c1f7aaf1edd8ff&id=9d740459d3&subscribe=Subscribe&EMAIL=" + this.email;
  this.isSuccess = false;

  this.jsonp.request(url, this.headers).subscribe(response => {
    console.log(response);
    this.isSuccess = true; 
  });   
}
Run Code Online (Sandbox Code Playgroud)

不要忘记JSONP_PROVIDERS在调用bootstrap函数时指定.

有关详细信息,请参阅此链接: