【问题标题】:AngularJS app in GAE gets CORS errorGAE 中的 AngularJS 应用程序出现 CORS 错误
【发布时间】:2017-08-22 18:21:36
【问题描述】:

我有许多基于 GAE for Python 的应用程序和一个基于 AngularJS 的应用程序。其他的工作正常,AngularJS 的工作正常。现在我收到以下错误:

XMLHttpRequest 无法加载 xxxxxxxxx/rest/loadlocale?locale=en。 从 'xxxxxxxxxxxxxxx/rest/loadlocale?locale=en' 重定向到 'https://www.google.com/accounts/ServiceLogin?service=ah&passive=true&...... 已被 CORS 策略阻止:没有“访问控制允许来源” 请求的资源上存在标头。原点 'xxxxxxxx' 是 因此不允许访问。

如果我从 Chrome 显式转到 ServiceLogin,该应用程序将在一段时间内正常运行。过了一会儿(我假设登录过期)错误再次出现。

app.yaml 有以下部分:

- url: /rest/.*
  script: main.APP
  login: required
  secure: always

但删除“secure: always”没有区别。

错误发生在第一个客户端 http 请求时,但请求处理程序的 Python 服务器代码没有显示任何迹象 访问。

【问题讨论】:

    标签: angularjs google-app-engine cors


    【解决方案1】:

    这是因为您的 app.yaml 中有 login: required,这意味着所有 XMLHttpRequest 请求都需要有效的会话。

    GAE 的 login 由 Google 帐户处理。所以你不能改变你的设置。

    尝试从app.yaml 中删除login。如果您只希望在没有有效 Google 会话的情况下允许某些请求,您需要指定允许和不允许的内容。例如:

    - url: /rest/loadlocale
      script: main.APP
      login: optional # this is the default value and can be removed.
      secure: always
    
    - url: /rest/.*
      script: main.APP
      login: required
      secure: always
    

    【讨论】:

    • 抱歉,没有这样做。同样的错误。我注意到其他运行良好的应用程序有时也有 302(重定向)响应代码。所以其他事情正在发生。难道是因为它来自 XXXX.appspot.com/rest 而不是 XXXX.appspot.com 会触发 CORS 错误?
    猜你喜欢
    • 2016-08-15
    • 2017-02-18
    • 2017-06-27
    • 2015-12-09
    • 2021-11-16
    • 2011-01-17
    • 1970-01-01
    • 2014-12-25
    • 2020-11-29
    相关资源
    最近更新 更多