(状态: 302)Gitlab Pages JSON 翻译文件 CORS 策略错误和重定向

(Status: 302) Gitlab Pages JSON translation files CORS policy error & redirect

提问人:Lorenzo 提问时间:9/19/2023 更新时间:9/22/2023 访问量:123

问:

我有一个 Angular 项目,其中我使用 JSON 文件(路径:src/assets/i18n)将内容翻译成不同的语言。我将我的项目放在 Gitlab Pages 上,但出现以下错误:

CORS 策略阻止了从源“https://lorenzoname.gitlab.io”对“https://gitlab.com/oauth/authorize?client_id=5059a88907e1b093c23df47d996183b101a862a5e53e099b563120d1308db2c1&redirect_uri=https://projects.gitlab.io/auth&response_type=code&state=yjNh6jjcpRaRwWxD1IGehQ==&scope=api”处的 XMLHttpRequest(从“https://lorenzoname.gitlab.io/assets/i18n/nl.json”重定向)的访问:CORS 策略上不存在“Access-Control-Allow-Origin”标头请求的资源

我还收到以下错误代码:

状态代码:302 => 没有可用的内容,因为此请求是 重 定向。

后跟两个请求:

请求URL:https://projects.gitlab.io/auth?domain=https://lorenzoname.gitlab.io&state=MTUxyEVL9u1G1ytX0KbRrQ== 请求方式:GET 状态码:302 引荐来源政策:strict-origin-when-cross-origin

和:

请求URL:https://gitlab.com/oauth/authorize?client_id=5059a88907e1b093c23df47d996183b101a862a5e53e099b563120d1308db2c1&redirect_uri=https://projects.gitlab.io/auth&response_type=code&state=MTUxyEVL9u1G1ytX0KbRrQ==&scope=api 请求方式:GET 状态码:302 引荐来源政策:strict-origin-when-cross-origin

我的gitlab-ci.yml工作输出:

output gitlab-ci

是否有某种配置可以使文件不被阻止,或者是否有一些权限需要更改?

gitlab cors http-status-code-302 gitlab-pages

评论


答:

0赞 Lorenzo 9/22/2023 #1

我设法用app.module.ts文件中的以下代码解决了这里的问题:

export function HttpLoaderFactory(http: HttpClient) {
  return new TranslateHttpLoader(http, "./assets/i18n/", ".json");
}

我最初有相同的代码,但没有“./assets/i18n/”、“.json”部分。

对于那些对 /assets/i18n/ 文件夹中的 JSON 翻译文件有同样问题的人,请不要忘记将 angular.json 文件编辑为:

          "assets": [
            "src/favicon.ico",
            "src/assets",
            {
              "glob": "*.json",
              "input": "src/assets/i18n/",
              "output": "./assets/i18n/"
            }
          ],