加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angular获取restful数据跨域问题

发布时间:2020-12-17 08:43:22 所属栏目:安全 来源:网络整理
导读:本地搭建demo的时候,angular项目默认部署在4200端口,而后台服务器部署在8080端口; 当用angular发送请求到服务器的时候会报跨域问题 解决方案是在中间加一层nginx 配置如下 nginx: location /api { proxy_pass http://localhost:8080/angular/demo;} angu

本地搭建demo的时候,angular项目默认部署在4200端口,而后台服务器部署在8080端口; 当用angular发送请求到服务器的时候会报跨域问题

解决方案是在中间加一层nginx

配置如下 nginx:

location /api {
    proxy_pass   http://localhost:8080/angular/demo;
}

angular增加配置文件 proxy.conf.json

{
  "/api": {
    "target": "http://localhost","secure": false
  }
}

启动angular项目的时候 使用 ng serve --proxy-config proxy.conf.json

这样 angular 请求 http://localhost:4200/api/getList会被解析为 http://localhost/api/getList

nginx接收到请求,转发到 http://localhost:8080/angular/demo/getList

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读