第一步:在项目根目录(与package.json文件平级)创建文件proxy.conf.json,加入以下配置信息:
下面是假设post请求后台传的接口地址:
http://172.21.7.49:8080/stockanls/stockHomePage/queryViolationOfTheMonitor?userid=100055&busidate=0701
{"/stockanls": {// 匹配以/stockanls/开头的url路径"target": "http://172.21.7.49:8080", // 设置代理端口"secure": false,"changeOrigin": true,"cookieDomainRewrite": {"*": "localhost"},"cookiePathRewrite": {"*": "/"}}}
第二步:配置anjular.json:
找到 项目根目录下的 angular.json 文件,找到 projects节点->architect节点->serve节点->option节点追加内容:"proxyConfig": "proxy.conf.json"注:proxy.conf.json这个文件是你建的那个文件名修改之后的serve 节点内容如下:"serve": {"builder": "@angular-devkit/build-angular:dev-server","options": {"browserTarget": "PayLife:build","proxyConfig": "proxy.conf.json" //追加的配置代码},"configurations": {"production": {"browserTarget": "PayLife:build:production"}}}
**第三步:在package.json中加入proxy.config.json **.
"scripts": {"ng": "ng","start": "ng serve --proxy-config proxy.conf.json",
**第四步:发送请求 **.
var api = "/stockanls/stockHomePage/queryScale?userid=100050&busidate=0101";this.http.post(api,this.httpOptions).subscribe((response:any) => {this.lis1 = response});
注:配置完后需要重新启动项目
如果觉得《angular 跨域问题 proxy.conf.json》对你有帮助,请点赞、收藏,并留下你的观点哦!