.NET CORE 2.0 Angular 5:允许使用Cors [英] .NET CORE 2.0 Angular 5: Allowing Cors

查看:78
本文介绍了.NET CORE 2.0 Angular 5:允许使用Cors的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用cors时遇到了一些麻烦.我已经这样设置了server side:

I am having some trouble allowing cors. I have set server side like so:

  app.UseCors(builder => builder.WithOrigins("http://localhost:4200/").AllowAnyHeader());

startup类的configure方法内部

当点击我的Web API时,它将返回正确的数据.

When the my web API is hit, it will return the data fine.

但是,问题似乎出在Angular上,因为出现以下错误:

However, the problem seems to be with Angular as in the I get the following error:

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.

这是我的有角度的Web api调用

Here is my angular web api call

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';;


@Injectable()
export class ProfileService {

    private baseUrl = 'api/profile/';

    constructor(private http: HttpClient) { }

    getLookups(step: number) {
        return this.http.get('http://localhost:51658/' + this.baseUrl + 'lookups/' + step)
    }

}

推荐答案

builder.WithOrigins("http://localhost:4200/")更改为

builder.WithOrigins("http://localhost:4200")

(删除了"/")

这篇关于.NET CORE 2.0 Angular 5:允许使用Cors的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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