angular中http中请求数据跨域的问题
我是使用代理做的请求数据
1 首先需要建立一个JSON文件,文件名为proxy.config.json:
2配置内容:如下 proxy.config.json:
{
"/api": {
"target": "http://localhost:3001",
"secure": false,
"pathRewrite": {
"^/api": ""
}
}
}
3 然后配置package.json文件:
"start": "ng serve --proxy-config proxy.conf.json",
4 使用方法:
http://localhost:3001为服务器的IP地址,或者是所需要请求的接口域名,该地址就是需要被代理的
this.http.get("/api").subscribe((res:any)=>{
console.log('数据的返回',res)
})