且构网

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

Angular 5:飞行前响应具有无效的HTTP状态代码403

更新时间:2022-06-27 05:54:53

问题:

对于任何跨域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",内容类型,如果不匹配");
  • response.setHeader("Access-Control-Allow-Methods","POST,GET,OPTIONS");
  • response.setHeader("Access-Control-Allow-Origin","*");
  • response.setHeader("Access-Control-Max-Age","3600");