【问题标题】:Error Code: 0 ExceptionMessage: Http failure response for (unknown url): 0 Unknown Error in Angular 5错误代码:0 ExceptionMessage:(未知 url)的 Http 失败响应:0 Angular 5 中的未知错误
【发布时间】: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


【解决方案1】:

由于 CORS 错误,您的浏览器不允许您的代码读取 HTTP 响应。

您将在浏览器控制台日志中看到错误(Chrome 显示)

  1. 如果 OPTIONS 请求得到正确处理,请检查 AWS ApiGateway
  2. 在“网络”选项卡中(如果您使用的是 Chrome),检查飞行前请求
  3. 您必须检查access-control-allow-origin 发送的标头参数 网络日志中的服务器(响应标头)并确保它是 正确配置

如果没有任何效果,请删除 API 网关配置并重新创建它们(如果您有云形成脚本会很容易)。它总是有效的

【讨论】:

  • 这个问题只发生在少数用户身上,如果我现在测试它就可以了。由于它不可重现,因此很难对其进行测试。我还检查了您在评论中提到的所有内容,但仍然没有运气。
  • 这种情况下,检查当HTTP_STATUS不是200时是否正确返回了access-control-origin。也就是说,在发生错误时检查。默认情况下,发生错误时 AWS 网关不会发送 CORS 标头。将 CORS 添加到 4xx 和 5xx 或返回的任何代码
  • 可能是遇到这个问题的用户没有正确获取 CORS
  • 是的,但无法理解为什么只有少数用户面临 CORS 问题而其他用户即使使用相同的功能和相同的 URL 也没有。我确认这也与浏览器无关,所以绝对是 CORS 问题,但看起来很奇怪。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-29
  • 2018-12-20
  • 2019-01-29
  • 1970-01-01
相关资源
最近更新 更多