CORS 策略已阻止从来源 'http://localhost:4200' 访问位于 'URL' 的 XMLHttpRequest:
Access to XMLHttpRequest at 'URL' from origin 'http://localhost:4200' has been blocked by CORS policy:
我以为我已经允许访问所有来源,但我仍然收到此错误。
Access to XMLHttpRequest at 'localhost:60248/api/page/1' from origin
'http://localhost:4200' has been blocked by CORS policy: Cross origin
requests are only supported for protocol schemes: http, data, chrome,
chrome-extension, https.
UPDATE 添加建议的代码更改后,错误现已更改。现在是这样的:
Access to XMLHttpRequest at 'http://localhost:60248/api/page/1' from
origin 'http://localhost:4200' has been blocked by CORS policy:
Response to preflight request doesn't pass access control check:
Redirect is not allowed for a preflight request.
在 Startup.cs 我有这个:
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy(name: "AllowAll",
builder =>
{
builder.AllowAnyOrigin()
.WithMethods("PUT", "DELETE", "GET");
});
});
在我的控制器中我有这个:
[Route("api/[controller]")]
[ApiController]
public class PageController : ControllerBase
{
...
[EnableCors("AllowAll")]
[HttpGet("{id:int}")]
public async Task<IActionResult> GetPageData(int id)
{
...
这是调用端点的 Angular 服务:
export class PageContentService {
constructor(private http: HttpClient) { }
getContent(id: number): Observable<PageContent> {
let headers = new HttpHeaders();
headers = headers.set('Content-Type', 'application/json');
const httpOptions = {
headers: headers
}
return this.http.get<PageContent>(`http://localhost:60248/api/page/${id}`, httpOptions);
}
}
我以为这样就可以了,但显然不行。
我还需要做什么?
您还需要添加中间件。
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
...
app.UseCors();
...
}
我想你还需要两件事:
如 vivek 所说,在您的 Configure
方法中将 .UseCors()
添加到您的请求管道。
将 .AllowAnyHeader()
添加到策略构建器中的 CORS-Config,我认为默认情况下不允许使用 Content-Type
-Header。
我以为我已经允许访问所有来源,但我仍然收到此错误。
Access to XMLHttpRequest at 'localhost:60248/api/page/1' from origin 'http://localhost:4200' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.
UPDATE 添加建议的代码更改后,错误现已更改。现在是这样的:
Access to XMLHttpRequest at 'http://localhost:60248/api/page/1' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request.
在 Startup.cs 我有这个:
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy(name: "AllowAll",
builder =>
{
builder.AllowAnyOrigin()
.WithMethods("PUT", "DELETE", "GET");
});
});
在我的控制器中我有这个:
[Route("api/[controller]")]
[ApiController]
public class PageController : ControllerBase
{
...
[EnableCors("AllowAll")]
[HttpGet("{id:int}")]
public async Task<IActionResult> GetPageData(int id)
{
...
这是调用端点的 Angular 服务:
export class PageContentService {
constructor(private http: HttpClient) { }
getContent(id: number): Observable<PageContent> {
let headers = new HttpHeaders();
headers = headers.set('Content-Type', 'application/json');
const httpOptions = {
headers: headers
}
return this.http.get<PageContent>(`http://localhost:60248/api/page/${id}`, httpOptions);
}
}
我以为这样就可以了,但显然不行。
我还需要做什么?
您还需要添加中间件。
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
...
app.UseCors();
...
}
我想你还需要两件事:
如 vivek 所说,在您的
Configure
方法中将.UseCors()
添加到您的请求管道。将
.AllowAnyHeader()
添加到策略构建器中的 CORS-Config,我认为默认情况下不允许使用Content-Type
-Header。