我使用的是angular2和typescript。
我正试着发邮件到我的邮件黑猩猩订阅列表。
到目前为止,我的代码:
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;
});
}
这是我在控制台中得到的错误:
我现在收到这个错误:未捕获SyntaxError:意外的令牌<
当前代码如下:
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;
});
}
发布于 2016-02-02 22:14:02
Access-Control-Allow-Origin
报头应该出现在响应中,而不是请求中。
如果您的服务支持CORS,则必须在响应头中返回它才能允许请求。因此,这不是Angular应用程序的问题,而是必须在服务器端级别处理的问题……
如果您需要更多详细信息,可以查看此链接:
了解和使用CORS - http://restlet.com/blog/2015/12/15/understanding-and-using-cors/
编辑
似乎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;
});
}
在调用bootstrap
函数时,不要忘记指定JSONP_PROVIDERS
。
有关更多详细信息,请参阅此链接:
发布于 2017-07-03 21:45:14
问题出在服务器端。您必须告诉您的服务接受GET请求。例如,在使用Spring的JEE中,您只需添加:
@CrossOrigin
@RequestMapping(value = "/something", method = RequestMethod.GET)
https://stackoverflow.com/questions/35155752
复制相似问题