Angular2 CORS问题 [英] Angular2 CORS issue

查看:756
本文介绍了Angular2 CORS问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的angular2和公平我有很少的知识,我尝试修复,但我遇到一些问题有关跨站点请求,尝试从另一个应用程序访问服务,但我有这个问题无论我尝试做什么

I'm new to angular2 and to be fair I have very few knowledges which I try to fix, however I've run into some issues about cross site request, trying to access a service from another application but I have this issue whatever I try to do

XMLHttpRequest cannot load https://hr/Team/EditEmployeeInfo.aspx. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:54396' is therefore not allowed access. The response had HTTP status code 401.

这是我的angular2服务,

This is my angular2 service and I've tried something like this

getUserHrtbProfile(userId): Promise<any> {            
        const headers = new Headers();
        headers.append('Access-Control-Allow-Headers', 'Content-Type');
        headers.append('Access-Control-Allow-Methods', 'GET, PUT, POST, DELET');
        headers.append('Access-Control-Allow-Origin', '*');

        var apiUri: string = "https://hrtb/Team/EditEmployeeInfo.aspx?emplid={0}&Menu=InfoEmployee&T=0".replace("{0}", userId);
        return this.http.get(apiUri, headers).map(result => result.json()).toPromise();
}

这是我的组件

this.bannerService.getUserHrtbProfile(this.userId).then(hrtbJson => {
    this.hasHrtbAccess = hrtbJson.HasHrtbAccess;
    this.hrtbProfileUrl = hrtbJson.HrtbProfileUrl;
}).catch(err => {
    this.hasHrtbAccess = false;
});

我在我的问题上搜索了一个解决方案,但仍然找不到一个适合我的需要。

I've search a solution on my problem but still could not find one that suits my need.


具有Access-Control-Allow-Origin设置为* 的Angular 2 http请求

Angular 2 http request with Access-Control-Allow-Origin set to *

但最重要的是,这是一个angular2问题,我需要解决?或者事实上我已经读过它应该是由暴露API的团队处理的?

谢谢大家。

But most important, is this an angular2 problem that I need to solve? Or in fact as I've read it should have been handled by the team that exposes the API?
Thank you all.

推荐答案

您尝试在其他网域上提出要求,这是您在这里无法解决的问题。请尝试在您支持的代码提出请求,这将解决您的问题。

You are trying to make request on other domain, this is what you can not resolve here. try with making request at you backed code, this will resolve you issue.

这篇关于Angular2 CORS问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆