本文介绍了.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("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 '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('localhost:51658/' + this.baseUrl + 'lookups/' + step) } }推荐答案
将builder.WithOrigins("localhost:4200/")更改为
builder.WithOrigins("localhost:4200")(删除了"/")
更多推荐
.NET CORE 2.0 Angular 5:允许使用Cors
发布评论