西周舍利 2021-06-04 17:54 采纳率: 81.8%
浏览 60
已结题

angular启动项目,前端跨域问题怎么解决

import { Component, OnInit } from '@angular/core';
import * as echarts from 'echarts';
import 'echarts-gl';
import * as $ from 'jquery';

@Component({
  selector: 'app-echarts-glreal',
  templateUrl: './echarts-glreal.component.html',
  styleUrls: ['./echarts-glreal.component.scss']
})
export class EchartsGLRealComponent implements OnInit {

  ROOT_PATH = 'https://echarts.apache.org/examples';

  constructor() { }

  ngOnInit(): void {
    this.initChart();
  }

  initChart() {

    const ec = echarts as any;

    const echartReal3D = ec.init(document.getElementById('echartReal3D'));

    $.get(this.ROOT_PATH +'data/asset/data/life-expectancy-table.json', function (data) {
      echartReal3D.setOption({
        grid3D: {},
        xAxis3D: {},
        yAxis3D: {},
        zAxis3D: {},
        dataset: {
          source: data
        },
        series: [
          {
            type: 'scatter3D',
            symbolSize: 2.5
          }
        ]
      })
    });

  }
}
Access to XMLHttpRequest at 'https://echarts.apache.org/examples/data/asset/data/life-expectancy-table.json' from origin 'http://localhost:4200' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header has a value 'https://echarts.apache.org' that is not equal to the supplied origin.
echarts.apache.org/examples/data/asset/data/life-expectancy-table.json:1
Failed to load resource: net::ERR_FAILED
  • 写回答

1条回答 默认 最新

  • 小P聊技术 2021-06-05 09:49
    关注

    在进行项目开发过程中,经常会遇到跨域问题,下面就通过一些配置进行解决跨域问题。

     

    1.在项目根目录下进行创建文件 proxy.config.json;

    {
        "/": {
            "target": "", //要指向的域名或端口号
            "logLevel": "debug", //
            "secure": false, //
            "changeOrigin": true, //
            "pathRewrite": {
                "^/": ""
            }
        }
    }

    2. 修改启动文件:

    修改angular.json的配置文件:

    {
          "projects": {
            "my-app": {
              "architect": {
                "serve": {
                  "builder": "@angular-devkit/build-angular:dev-server",
                  "options": {
                    "browserTarget": "my-app:build",
                    "proxyConfig": "proxy.config.json"
                  },
                  "configurations": {
                    "production": {
                      "browserTarget": "my-app:build:production"
                    }
                  }
                }
              }
            }
          }
        }
    }

    不修改angula.json文件,修改package.json文件的start命令;

    {
        "name": "my-app",
        "version": "0.0.0",
        "scripts": {
            "ng": "ng",
            "start": "ng serve --proxy-config proxy.config.json",
            "build": "ng build",
            "test": "ng test",
            "lint": "ng lint",
            "e2e": "ng e2e"
        }
    }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 10月6日
  • 已采纳回答 9月28日

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵