Axios request interface, requested the solution twice

Have you ever encountered this kind of situation? When using axios to request an interface, the request was made twice,
insert image description here
insert image description here
respectively, the options request and the post request we expected.
Here we only talk about the solution. As for why there is an extra options request, if you are interested, you can find out by yourself on Baidu.

Solution:
Step 1:
Set the "Content-Type" of the request header of axios to "application/x-www-form-urlencoded;charset=UTF-8"

axios.defaults.headers["Content-Type"] = "application/x-www-form-urlencoded;charset=UTF-8";

Step 2:
The carrier of the request parameter uses URLSearchParams

  const params = new URLSearchParams();
  params.append(key, value);
  axios.post(url, url_params);

OK~, it only takes two steps to optimize the request twice

Guess you like

Origin blog.csdn.net/u010775335/article/details/127409327