首页 > 解决方案 > 将 Angular 项目部署到 CPanel 返回错误 Cors-Policy:请求源中不存在 Access-Control Allow-Origin Header

问题描述

这是我第一次在在线服务器上部署 Angular 应用程序。这不是企业的事情。在部署期间,我使用了这个命令

ng build --baseHref=https://www.exampleUrl.com/ --deployUrl=https://www.exampleUrl.com/

然后我把它上传到CPanel。

  1. 在最初的几分钟内,我可以在我的 PC 中看到我的网络,其绝对网址为“www.exampleUrl.com”,但移动设备无法访问该应用程序(idk 以查看移动设备中的控制台登录,因此我无法提供日志问题)。
  2. 一小时后,它返回此错误
Access to XMLHttpRequest at 'https://www.exampleUrl.com/ngx_pagespeed_beacon?url=https%3A%2F%2FexampleUrl.com%2F' from origin 'https://exampleUrl.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

其次是

www.exampleUrl.com/ngx_pagespeed_beacon?url=https%3A%2F%2FexampleUrl.com%2F:1 Failed to load resource: net::ERR_FAILED

我一直在这个论坛中浏览整个相关标签,但它们都不适合我。任何评论将不胜感激!

顺便说一句,这是我的 package.json 和 angular.json(也许你们需要观察这些)

{
  "name": "myproject",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "watch": "ng build --watch --configuration development",
    "test": "ng test"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~12.1.1",
    "@angular/cdk": "^12.2.0",
    "@angular/common": "~12.1.1",
    "@angular/compiler": "~12.1.1",
    "@angular/core": "~12.1.1",
    "@angular/forms": "~12.1.1",
    "@angular/material": "^12.2.0",
    "@angular/platform-browser": "~12.1.1",
    "@angular/platform-browser-dynamic": "~12.1.1",
    "@angular/router": "~12.1.1",
    "bootstrap": "^4.5.3",
    "ngx-bootstrap": "^7.0.0",
    "rxjs": "~6.6.0",
    "tslib": "^2.2.0",
    "zone.js": "~0.11.4"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~12.1.1",
    "@angular/cli": "~12.1.1",
    "@angular/compiler-cli": "~12.1.1",
    "@types/jasmine": "~3.6.0",
    "@types/node": "^12.11.1",
    "jasmine-core": "~3.7.0",
    "karma": "~6.3.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage": "~2.0.3",
    "karma-jasmine": "~4.0.0",
    "karma-jasmine-html-reporter": "^1.5.0",
    "typescript": "~4.3.2"
  }
}

角.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "cli": {
    "analytics": "f6871353-1796-4510-ac5d-c072d98cf307"
  },
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "myproject": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "style": "scss"
        },
        "@schematics/angular:application": {
          "strict": true
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/myproject",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "inlineStyleLanguage": "scss",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.scss"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "500kb",
                  "maximumError": "1mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "2kb",
                  "maximumError": "4kb"
                }
              ],
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "outputHashing": "all"
            },
            "development": {
              "buildOptimizer": false,
              "optimization": false,
              "vendorChunk": true,
              "extractLicenses": false,
              "sourceMap": true,
              "namedChunks": true
            }
          },
          "defaultConfiguration": "production"
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "configurations": {
            "production": {
              "browserTarget": "myproject:build:production"
            },
            "development": {
              "browserTarget": "myproject:build:development"
            }
          },
          "defaultConfiguration": "development"
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "myproject:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.spec.json",
            "karmaConfig": "karma.conf.js",
            "inlineStyleLanguage": "scss",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.scss"
            ],
            "scripts": []
          }
        }
      }
    }
  },
  "defaultProject": "myproject"
}

标签: angularcpanel

解决方案


根据您使用的服务器,您需要允许请求源自的域向相关服务器发出请求。

要允许所有人,您将执行类似“Access-Control-Allow-Origin”“*”之类的操作

“*”选项允许来自所有域的请求,您也可以指定特定域。最好不要允许来自所有域的请求。

下面是我用 Java 编写并在 Jetty Server 上运行的一个旧项目的摘录。

return responseBuilder.header("Access-Control-Allow-Origin", "*").build();


推荐阅读