我正在关注这个项目 angular2-authentication-sample用于 angular2 js 进行身份验证。我遇到这个问题 Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8000/api/auth/register. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).
当向注册端点请求时。
我检查了不同的问题,并在 headers 中添加了额外的标题.
headers.ts
import { Headers } from '@angular/http';
export const contentHeaders = new Headers();
contentHeaders.append('Accept', 'application/json');
contentHeaders.append('Content-Type', 'application/json');
contentHeaders.append('Access-Control-Allow-Origin', '*');
contentHeaders.append('Access-Control-Request-Method', '*');
contentHeaders.append('Access-Control-Allow-Methods', '');
contentHeaders.append('Access-Control-Request-Headers', 'Origin, Content-Type, Accept');
我无法解决这个 CROS 问题。
最佳答案
这需要在服务器端解决,提供您从本地主机 Angular 应用程序调用的任何服务的服务器。
如果我理解您的描述,问题是您正在尝试在客户端应用程序中添加 header 配置。但为了允许 CORS 共享,应在服务器上添加一些 header 。
The Cross-Origin Resource Sharing standard works by adding new HTTP headers that allow servers to describe the set of origins that are permitted to read that information using a web browser.
关于javascript - 跨源请求被阻止 : The Same Origin Policy disallows reading the remote resource at http://xxx:xx/register,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39761779/