【问题标题】:angular6 http requests returns isTrustedangular6 http 请求返回 isTrusted
【发布时间】:2018-12-04 13:51:12
【问题描述】:

我有一个 angular6 应用程序在生产中运行,偶尔有一些用户在 http 请求期间遇到错误。

Http failure response for (unknown url): 0 Unknown Error response: {"isTrusted":true}

这会随机影响用户,用户的 HTTP 方法或其他任何东西都没有模式,据我所知,我使用 sentry 记录错误。

我已经花了很多时间寻找解决方案,到目前为止,几乎所有内容都暗示了错误的 cors 标头。 所有请求都通过一个设置了 cors 标头的 api 网关。

'Access-Control-Allow-Headers': 'Content-Type, x-internal-token, Origin, Accept, X-Requested-With, If-Modified-Since, Cache-Control, Keep-Alive'
'Access-Control-Allow-Origin': 'http://example.com'
'Access-Control-Allow-Methods': 'PUT, POST, GET, DELETE, PATCH, OPTIONS'
'Access-Control-Max-Age': 3600

到目前为止,当应用程序首次加载并从 api 网关获取一些信息时,我没有收到任何错误,只是稍后在用户使用应用程序时。 更奇怪的是,./assets/i18n/de.json(正常获取请求)也出现了这个错误,这不是 cors,而只是一些用于动态翻译的静态 json。

我完全没有想法,非常感谢任何帮助。

编辑:请仔细阅读;这个问题只对某些用户存在,而不是一直存在,这不是一般的错误配置!

EDIT2:为了进一步调试这个问题,我设置了第二个 API 网关(相同的代码),它被配置为记录所有请求。对 Angular 应用程序进行了轻微修改,因此它会执行两次相同的请求;一次针对真正的 api-gateway,一次针对日志记录 api-gateway(对于某些 api 调用)。在一种情况下,应用程序能够向真正的 api-gateway 发出请求,但不能向日志网关发出请求(相同的代码,nginx、cors 标头都相同)。

EDIT3:日志网关和真正的 api-gateway 位于不同的服务器(不同的提供者)上,我可以在 nginx 日志中看到状态为 200 的 OPTIONS 请求。

EDIT4:我已将 cors 处理从 api-gateway 移至 nginx,到目前为止我还没有收到更多错误。

【问题讨论】:

  • 检查这些解决方案是否有帮助。 stackoverflow.com/questions/47180634/…
  • 谢谢,已经看到了,但没有任何解决方案有帮助。
  • 问题中引用的错误信息与CORS无关
  • 您是否尝试关闭 AdBlock?
  • 有什么方法可以验证遇到这种情况的用户是否被剥夺了 CORS 标头?根据公司网络设置,这可以解释为什么只有部分用户会体验到它。

标签: angular rest http production-environment


【解决方案1】:

似乎是 Nginx 配置问题。 以下 Nginx 代码将添加 HTML 标头响应 Access-Control-Allow-Origin: * : public web static files of domain 让其他域访问这些 web 静态文件没有问题:

location / {
  location ~* ^.+\.(?:css|cur|json|js|jpeg|gif|htc|ico|png|txt|otf|ttf|eot|woff|svg|webp|webm|zip|gz|tar|rar)$ {
    # If request comes from allowed subdomain
    # (yourdomain.com) then we enable CORS
    # if ($http_origin ~* (https?://yourdomain\.com(:[0-9]+)?$)) {
    #  set $cors "1";
    # }

    set $cors "1";

    # OPTIONS indicates a CORS pre-flight request
    if ($request_method = 'OPTIONS') {
      set $cors "${cors}o";
    }

    # Append CORS headers to any request from 
    # allowed CORS domain, except OPTIONS
    if ($cors = "1") {
      more_set_headers 'Access-Control-Allow-Origin: $http_origin';
      more_set_headers 'Access-Control-Allow-Credentials: true';
    }

    # OPTIONS (pre-flight) request from allowed 
    # CORS domain. return response directly
    if ($cors = "1o") {
      more_set_headers 'Access-Control-Allow-Origin: $http_origin';
      more_set_headers 'Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE';
      more_set_headers 'Access-Control-Allow-Credentials: true';
      more_set_headers 'Access-Control-Allow-Headers: Origin,Content-Type,Accept';
      add_header Content-Length 0;
      add_header Content-Type text/plain;
      return 204;
    }
  }
}

【讨论】:

    【解决方案2】:

    我认为,这将解决问题。在 json 文件中,请更新 assets 属性以包含“array”而不是“string”。

    这是 Angular CLI beta 版本与稳定版本的问题。

    "apps": [
        {
          "root": "src",
          "outDir": "dist",
          "assets": "assets"
        }
    ]
    
     "apps": [
        {
          "root": "src",
          "outDir": "dist",
          "assets": [
            "assets"
          ]
        }
    ]
    

    【讨论】:

    • & 尝试使用 Access-Control-Allow-Headers: * Access-Control-Request-Headers: * Access-Control-Request-Method: GET 而不是 'Access-Control-Allow-Headers': 'Content-Type, x-internal-token, Origin, Accept, X-Requested-With, If-Modified-Since, Cache-Control, Keep-Alive' & 保持所有其他标题与上面写的相同。
    猜你喜欢
    • 1970-01-01
    • 2011-12-10
    • 2020-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-28
    • 2017-02-22
    • 2015-09-27
    相关资源
    最近更新 更多