angular http 跨域访问
1、在跟目录中创建配置文件:proxy.config.json ,文件内容如下:
{ "/api": { "target": "http://192.168.0.200:10091", "secure": false, "pathRewrite": { "^/api": "" } } }
2、修改 package.json 配置文件内容,内容修改如下:
"start": "ng serve --proxy-config proxy.config.json",
3、启动项目
npm run start
3、angular http 请求:
this.http.get("/api/queryitem").subscribe((res: any) => { console.log(res); });
请求的时候,会自动将 /api 替换成 http://192.168.0.200:10091,在浏览器调试中网络监控中显示的会依旧是网站自身的域名。