且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Angular 5:预检响应具有无效的 HTTP 状态代码 403

更新时间:2022-05-08 05:34:52

问题:

对于任何跨域 POST 请求,浏览器将首先尝试执行 OPTIONS 调用,并且当且仅当该调用成功时,它才会执行真正的 POST 调用.但在您的情况下, OPTIONS 调用失败,因为没有 'Access-Control-Allow-Origin' 响应标头.因此,实际调用将不会完成.

For any Cross-Origin POST request, the browser will first try to do a OPTIONS call and if and only if that call is successful, it will do the real POST call. But in your case, the OPTIONS call fails because there is no 'Access-Control-Allow-Origin' response header. And hence the actual call will not be done.

SLOUTION :

为此,您需要在服务器端添加 CORS 配置以设置跨源请求所需的适当标头,例如:

So for this to work you need to add CORS Configuration on the server side to set the appropriate headers needed for the Cross-Origin request like :

  • response.setHeader("Access-Control-Allow-Credentials", "true");
  • response.setHeader("Access-Control-Allow-Headers", "content-type, if-none-match");
  • response.setHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS");
  • response.setHeader("Access-Control-Allow-Origin", "*");
  • response.setHeader("Access-Control-Max-Age", "3600");