Angular5 角度->;钥匙斗篷编号';访问控制允许原点';

Angular5 角度->;钥匙斗篷编号';访问控制允许原点';,angular5,Angular5,我正在尝试连接到Keyckloak,以使用angular获取“访问令牌”。但我有一个错误: 加载失败:请求的资源上不存在“Access Control Allow Origin”标头。因此,不允许访问源“”。响应的HTTP状态代码为400 我给你我的密码: readonly ROOT_URL = 'http://localhost:8080/auth/realms/demo/protocol/openid-connect/token'; posts : any; constructor

我正在尝试连接到Keyckloak,以使用angular获取“访问令牌”。但我有一个错误:

加载失败:请求的资源上不存在“Access Control Allow Origin”标头。因此,不允许访问源“”。响应的HTTP状态代码为400

我给你我的密码:

readonly ROOT_URL = 'http://localhost:8080/auth/realms/demo/protocol/openid-connect/token';
posts : any;    

constructor(private http: HttpClient) {}

getToken() {
const data: Post = {
username: 'pierrecolart',
password: 'root',
grant_type: 'password',
client_id: 'admin-cli'
}
this.http
    .post(
        this.ROOT_URL,
        data, 
        {headers: new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded')}
    )
    .map(res => res.json())
    .subscribe(data => console.log(data))
}

CORS访问问题,您需要在api服务器上允许CORS。通常,当API和前端在不同的域8080和4200上运行时,您会遇到此问题。这是浏览器默认的安全性,但您实际上未能提供有关后端语言的信息


查看这篇文章中的Nodejs,它应该足够有用

假设您使用的是express,您必须在服务器文件中允许CORS:

`var $app = express();
var allowCrossDomain = function(req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
    res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
    next();
};
$app.use(allowCrossDomain);`

所以问题在于我使用了2个localhost?没错,通常当你这样做的时候,你只需要轻微的配置,它就可以工作了。什么是后端语言,以及您使用的SOAP或REST是哪种类型的API工作的谢谢,但我现在有一个错误:HttpErrorResponse{headers:HttpHeaders,status:400,statusText:“错误请求”,url:,ok:false,…}错误:错误:“无效请求”错误描述:“缺少表单参数:grant\u类型”proto:Object headers:HttpHeaders{normalizedNames:Map(0),lazyUpdate:null,lazyInit:ƒ}消息:“Http失败响应:400错误请求”名称:使用的服务器是keydepot,它不是可自定义的node.js服务器