我在 Stack Overflow 上看到过其他类似的问题,但到目前为止还没有对我有任何帮助。
我有一个服务于http://localhost:4200 的角度应用程序。我有一个后端http://localhost:8080/myApp/
我正在尝试通过以下调用设置从前端到达后端:
public getPeople(): Observable<PeopleModel[]> {
return this.http
.post(API_URL + '/getPeopleDetails', {})
.map(response => {
const people = response.json();
console.log(people);
return partners.map((people) => new PeopleModel(people));
})
.catch(this.handleError);
}
Run Code Online (Sandbox Code Playgroud)
API_URL 设置为http://localhost:8080/myApp
我的代理配置如下所示:
{
"/myApp/*": {
"target": "http://localhost:8080/myApp",
"secure": false,
"changeOrigin": true,
"logLevel": "debug",
"pathRewrite" : {"^/myApp" : "http://localhost:8080/myApp"}
}
}
Run Code Online (Sandbox Code Playgroud)
我的 package.json 有以下内容:
"start": "ng serve --proxy-conf proxy.conf.json",
Run Code Online (Sandbox Code Playgroud)
我在尝试运行该应用程序时收到的错误是 CORS 错误和 403。
Failed to load http://localhost:8080/myApp/getPeopleDetails: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The response had HTTP status code 403.
Run Code Online (Sandbox Code Playgroud)
我将不胜感激任何帮助。谢谢!
在与 package.json 文件平行的应用程序根目录中,创建一个新文件
proxy.config.json
{
"/api*": {
"target":"http://localhost:8080",
"secure":false,
"logLevel":"debug"
}
}
Run Code Online (Sandbox Code Playgroud)
现在,在脚本中的package.json中:{},添加以下文件名为proxy.config.json 的flax以启动”
{
"name":"xyz",
"version":"1.0.0",
"license":"MIT",
"angular-cli": {},
"scripts": {
"start":"ng serve --proxy-config proxy.config.json",
"test":"ng test"
}
Run Code Online (Sandbox Code Playgroud)
}
希望这对你有用。
| 归档时间: |
|
| 查看次数: |
23219 次 |
| 最近记录: |