簡體   English   中英

錯誤代碼:0 ExceptionMessage:Http 失敗響應(未知 url):0 Angular 5 中的未知錯誤

[英]Error Code: 0 ExceptionMessage: Http failure response for (unknown url): 0 Unknown Error in Angular 5

我在我的 Angular 5 項目中收到此錯誤“錯誤代碼:0 ExceptionMessage:Http 失敗響應(未知 URL):0 未知錯誤”。

但是,我無法重新創建此問題,我在 Rollbar 錯誤日志中看到了此重復問題。 我在本地機器上嘗試了相同的步驟,還嘗試在不同的瀏覽器(如 chrome、safari 等)中重新創建,但沒有運氣。

我正在使用 AWS lambda 和 .NET 核心 2.1、API 代理網關和 ZC31C335EF37283C1Z51B1 上的 Front8.

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();
  }

以下是我的 startup.cs 配置 function,我已經在使用 cors。

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();
        }
    }

由於 CORS 錯誤,您的瀏覽器不允許您的代碼讀取 HTTP 響應。

您將在瀏覽器控制台日志中看到錯誤(Chrome 顯示)

  1. 如果正確處理了 OPTIONS 請求,請檢查 AWS ApiGateway
  2. 在網絡選項卡中(如果您使用的是 Chrome),檢查飛行前請求
  3. 您必須檢查服務器在網絡日志(響應標頭)中發送的access-control-allow-origin header 參數並確保其配置正確

如果沒有任何效果,請刪除 API 網關配置並重新創建它們(如果您有雲形成腳本會很容易)。 它總是有效

響應標頭

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM