尝试执行此代码时:
deleteUser(id: number): Promise<void> {
return this.http.delete(GlobalVariables.REST_API_ADDRESS + '/users/' + id)
.toPromise()
.then(() => null);
}
我得到此错误:
Failed to load resource: the server responded with a status of 404 (Not Found)
users:1 XMLHttpRequest cannot load http://localhost:8080/users/1.
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 404.
但是邮递员没有任何合理的问题。
我可以从Angular中获取而没有问题(我已经将访问权利 - 允许 - 允许蛋白设置为 *在服务器中(。
我解决了这个问题,我实际上并不是在角度,问题在我的后端中。
我尝试添加此标头
w.Header().Set("Access-Control-Allow-Origin", "*")
在响应作者上,但这无效。您必须使用此软件包https://github.com/rs/cors,然后设置标题
c := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "DELETE", "UPDATE"},
})
handler := cors.Default().Handler(router)
handler = c.Handler(handler)
log.Fatal(http.ListenAndServe(":8080", handler))
它是在邮递员上工作的,因为它不是浏览器(duh!(,并且允许没有该标头的通信。