【发布时间】:2017-08-02 16:23:34
【问题描述】:
更新:虽然我在 AWS API Gateway api 上设置了 CORS,但我使用的是 Lambda 代理配置,这要求我将所需的标头放入 Lambda 处理程序中。
我想不出来这个。我收到 CORS 错误,同时获得响应正文(实际预期响应)。我正在使用 angular2/http。
XMLHttpRequest 无法加载 https://xxxxxx.execute-api.us-east-2.amazonaws.com/dev/search。不 请求中存在“Access-Control-Allow-Origin”标头 资源。因此不允许使用原点“http://localhost:4200” 访问。
知道这怎么可能吗??
代码(错误总是被捕获并打印出来,而我仍然得到适当的响应):
public getNotes(searchString: string, authtoken: string): Observable<NotesResponse> {
let headers = new Headers({'Content-Type': 'application/json'});
headers.append('Authorization', authtoken);
headers.append('search-key', searchString);
let options = new RequestOptions({headers: headers});
return this.http.post(this.notesUrl, null, options) // ...using post request
.map((res:Response) => res.json()) // ...and calling .json() on the response to return data
.catch((error:any) => Observable.throw(error.json().error || 'Server error')); //...errors if
}
this.com.notesService.getNotes(this.com.searchQuery, result).subscribe(
notes => {
console.log("Notes: notes");
},
err => {
// Log errors if any
console.log("There was an error retrieving the notes: " + err);
});
【问题讨论】:
-
这根本不能回答我的问题。我得到了实际的响应 json,但同时我得到了 CORS 异常。如果这只是一个 CORS 问题,我将无法在资源上发帖。
标签: angular http xmlhttprequest cors observable