What are the HTTP request modes?

methods instructions
GET The GET method requests a representation of a specified resource. Requests using GET should only be used to GET data.
POST The POST method is used to submit an entity to a specified resource, usually resulting in a state change on the server or.
PUT The PUT method replaces all current representations of the target resource with the request payload
DELETE Deleting a Specified resource
OPTIONS Communication options used to describe the target resource
HEAD The HEAD method requests the same response as the GET request, but without the response body. In short, the packet body is obtained
TRACE The TRACE method performs a message loopback test along the path to the target resource.
CONNECT The tunnel protocol link agent is required.
PATCH The PATCH method is used to apply partial changes to the resource.

Why is the options request sent?

But are you aware that options are a preflrequest but before the real request is sent, the browser sends a preflrequest called Options, which is used to query a method supported by the request server. But if the response is rejected, such as 404,500, The request doesn’t send a real request,

There are simple cross-domain requests and complex cross-domain requests. Simple cross-domain requests do not send options precheck requests. Complex cross-domain requests send an Options precheck request

Complex cross-domain requests are triggered when any of the following conditions are met:

  1. Request method notGET/POST/HEADwhen
  2. Content-type is not application/ X-www-form-urlencoded, multipart/form-data, or Text /plain
  3. The request sets a custom header field

The network in the console will make two requests, one is an options request, one is a real request, as follows:

Cross-domain resource sharing (CORS) can be set in app.js as follows:

app.all('*', function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); // res.header("Access-Control-Allow-Headers", "Content-Type, X-Requested-Withs, SessionToken"); res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS"); Res. header(" x-powered-by ", '3.2.1') res.header(" content-type ", "Application /json; charset=utf-8"); next(); });Copy the code

Ask big guys to give advice little younger brother a lot!