XML HTTP请求身份验证错误"被CORS策略阻止" - 角度

时间:2017-09-12 14:16:24

标签: angular authentication xmlhttprequest cors http-get

我正在尝试在我的角应用中显示来自Web服务器的信息,并且在运行时会出现此错误。这是因为用户名&访问应用程序需要密码。

Error:
XMLHttpRequest cannot load http://... Redirect from 'http://...' to 'http://.../login.html' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.

如何在我的代码中提供此功能以便加载? 我的代码:

project.viewer.component.ts:

@Component({
    selector: 'project-viewer',
    template: `<h1>Projects</h1>
                <pre>{{projects}}</pre>`,
    styleUrls: ['./app.component.css']
})

export class ProjectViewerComponent  {
    name = 'ProjectViewerComponent';
    projects: string;
    errorMessage = "";
    stateValid = true;

    constructor(private http:Http) {
            this.http.get("http://link/projects")
            .map(resp => resp.text())
            .subscribe(jsonAsText => this.projects = jsonAsText);
    }
}

如何在代码中对此进行身份验证? 提前谢谢。

0 个答案:

没有答案