【发布时间】:2018-12-03 03:37:51
【问题描述】:
当我尝试执行此 http 请求时,我正在部署和 angular 6 应用程序,该应用程序与 localhost 中的 tomcat 服务器一起工作
this.http
.post<LoginResult>( API_URL + '/login', JSON.stringify(json)/*, { headers: myHeader }*/).pipe(
catchError(this.handleError('get-token', []))).subscribe((response) => {
if(response['result_code'] == 'result_ok') {
this.auth.doSignIn(response['token'], response['name']);
this.router.navigate(['user_manager']);
return true;
}else {
return false;
}
});
everitying 效果很好,但是当我添加标题字段时
let myHeader = new HttpHeaders().append("Authorization", 'Basic' + this.session.getAccessToken());
this.http
.post<LoginResult>( API_URL + '/login', JSON.stringify(json), { headers: myHeader }).pipe(
catchError(this.handleError('get-token', []))).subscribe((response) => {
if(response['result_code'] == 'result_ok') {
this.auth.doSignIn(response['token'], response['name']);
this.router.navigate(['user_manager']);
return true;
}else {
return false;
}
});
这是我的输出错误:
Access to XMLHttpRequest at 'http://localhost:8095/login' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
HttpErrorResponse
我还检查了请求没有到达我的 tomcat 服务器,它之前被阻止,不允许角度检查响应头
感谢您的帮助
【问题讨论】:
-
两件事:不需要
JSON.stringify你的payload,因为Angular会为你做这件事。只需传递实际对象。此外,Basic和令牌值之间没有空格。 -
谢谢,但没有解决我的问题
标签: javascript node.js angular typescript tomcat