【发布时间】:2022-01-20 23:45:18
【问题描述】:
我在 Angular 5 项目中收到此错误“错误代码:0 ExceptionMessage:Http 失败响应(未知 URL):0 未知错误”。
但是,我无法重新创建此问题,我在 Rollbar 错误日志中看到了此重复问题。我在本地机器上尝试了相同的步骤,也尝试在不同的浏览器(如 chrome、safari 等)中重新创建,但没有成功。
我在前端使用带有 .NET core 2.1、API 代理网关和 Angular 5 的 AWS lambda。
AngularJs 代码
getOrgPublicAssetName(id: number): Observable<any> {
let content = this.appSharedService.getSitecontents();
if (content !== '')
return Observable.of(content);
if (id !== undefined)
return <Observable<any>>(
this.httpClient
.get(this.organizationUrl + '/SiteContent/' + id)
.map(result => {
this.appSharedService.setsitecontents(result);
return result;
})
.pipe(catchError(this.exceptionService.catchBadResponse))
);
else return Observable.of();
}
以下是我已经在使用 cors 的 startup.cs 配置功能。
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseXRay("Organizations");
app.UseMiddleware(typeof(ErrorHandlingMiddleware));
Environment.SetEnvironmentVariable(EnvironmentVariableAWSRegion.ENVIRONMENT_VARIABLE_REGION, Configuration["AWS:Region"]);
// Create a logging provider based on the configuration information passed through the appsettings.json
// You can even provide your custom formatting.
loggerFactory.AddLambdaLogger(Configuration.GetLambdaLoggerOptions());
//Enable swagger endpoint if the environment is not production
if (!IsProductionEnvironment)
{
// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger();
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c =>
{
if (env.EnvironmentName == "Local")
c.SwaggerEndpoint(SwaggerLocalEndpoint, ApiName);
else
c.SwaggerEndpoint(SwaggerEndpoint, ApiName);
});
}
app.UseCors(policy =>
{
//TODO: This has to be addressed once we deploy both api and ui under the same domain
policy.AllowAnyOrigin();
policy.AllowAnyHeader();
policy.AllowAnyMethod();
policy.WithExposedHeaders("WWW-Authenticate");
policy.WithExposedHeaders("X-Total-Count");
});
app.UseAuthentication();
app.UseMvc(routeBuilder =>
{
routeBuilder.Count().Filter().OrderBy().Expand().Select().MaxTop(null);
routeBuilder.MapODataServiceRoute("odata", "odata", OrganizationModelBuilder.GetEdmModel());
});
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
app.UseResponseCaching();
}
}
【问题讨论】:
-
您可以在发出请求时发布 Angular 代码吗?如果是前端错误,您应该在发出请求之前检查 URL 是否正常(例如,使用浏览器进行调试)。
-
@adrisons 已添加 Angular 代码,请检查
标签: angular amazon-web-services asp.net-core aws-lambda angular5