.NET CORE 2.0 Angular 5:允许使用Cors

编程入门 行业动态 更新时间:2024-10-24 00:18:41
本文介绍了.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

本文发布于:2023-11-10 09:37:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1575021.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:CORE   NET   Cors   Angular

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!