javascript - 跨源请求被阻止 : The Same Origin Policy disallows reading the remote resource at http://xxx:xx/register

标签 javascript angular

我正在关注这个项目 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 。

See MDN detailed explanation

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/

相关文章:

typescript - 如何访问 Angular2 指令上的组件?

angular - 如何测试Promise catch block 是否设置了this.error

angular - UpperCasePipe ngModel 不适用于输入中的最后一个字符?

javascript 在页面加载时更新 Joomla 文本字段

javascript - jquery 在满足两个条件时执行函数

javascript - 过渡不起作用

html - Angular - 选择占位符不显示

angular - HttpClient : Can´t access to response headers

javascript - Moment.js 本地相对时间

javascript - 检测宽度 : auto in jQuery