How to solve the cross-domain error of POST method (GET method is normal)?

I put the front-end static page on Ali Cloud"s oss object storage , and the server (node) runs on the CVM.
the front end needs cross-domain access to the server interface. The get method is normal, but the post method is wrong. A pre-request is sent before the post method. method is options . Does the server"s routing table need to add a corresponding OPTIONS route? do all post routes have to write the corresponding optons route? Please also give me some advice, please

the bandwidth of the PS: Tencent Cloud server is a small broken pipe of 1m, and there will be no cross-domain problem if the server is placed at the front end, but because the bandwidth is too small, the first screen has to wait for more than 10 seconds, so I think the server only does the function of the server, and the front end only adjusts the interface from the server.

get method is normal:
clipboard.png

post:
clipboard.png

nginx:
clipboard.png

node:

clipboard.png

Mar.28,2021

many cross-domain problems are solved through get.


you are 404 years old, so it should not be a cross-domain problem.


add withCredentials: true to the request try


originally I wanted to add a middleware to node. All requests that method are options are returned 204 . However, I think configuring nginx should also be possible and faster. I looked it up on the Internet and added this code

to nginx.conf .
if ($request_method = 'OPTIONS') {
  add_header 'Access-Control-Allow-Origin' '*';
  add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
  add_header 'Access-Control-Allow-Headers' 'Content-Type';
  add_header 'Access-Control-Max-Age' 1728000;
  add_header 'Content-Type' 'text/plain charset=UTF-8';
  add_header 'Content-Length' 0;
  return 204;
}

after the actual measurement, each post request will first initiate an options pre-request. After receiving the response from the server, it is determined whether to initiate the post request automatically according to the response information

.

clipboard.png


clipboard.png

clipboard.png

Menu