Why is HttpRequest sending the OPTIONS verb instead of POST?

前端 未结 2 681
醉话见心
醉话见心 2020-12-28 14:32

I got this code:

var req = new HttpRequest();
req.open(\"POST\", \"http://localhost:8031/rest/user/insert\");
req.se         


        
相关标签:
2条回答
  • 2020-12-28 14:48

    The OPTIONS verb is a preflight request sent by some browsers to check the validity of cross origin requests. It pretty much checks with the server that the Origin(requester) is allowed to make the request for a specified resource. Also, depending on which headers are sent back by the server it lets the browser know which headers, methods, and resources the origin is allowed to request form the server.

    The browser sends the OPTIONS request then if the server answers back with the correct headers (CORS headers) allowing the origin to make the request, you should see your POST request go through afterwards.

    Note that the CORS headers must be returned on both the OPTIONS response as well as the POST response. This means your server must be able to respond to the options method on the routes you want to access across domains.

    This is known as Cross-origin Resource Sharing. Mozilla has some pretty good documentation on the subject. https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS

    If you have more questions let me know and I'll answer them.

    0 讨论(0)
  • 2020-12-28 14:51

    One way to avoid this problem is by sending the request payload without custom headers and using formData to setup your request payload.

    0 讨论(0)
提交回复
热议问题