axios 为什么会出现POST 'backend endpoint' 400(Bad Request)错误?

ee7vknir  于 5个月前  发布在  iOS
关注(0)|答案(1)|浏览(89)

这段代码中的错误是:

const handleTokenRequest = async () => {
    try {
      const response = await axios.post(
        "backend endpoint here",
        {
          username: 1,
          password: 1,
          phone: formData.phoneNumber,
          verification_code: otp,
        },
        {
          headers: {
            "Content-Type": "application/json",
          },
        }
      );

      if (response.status === 200) {
        const accessToken = response.data.access;
        const refreshToken = response.data.refresh;

        loginUser(accessToken, refreshToken);
      } else {
        console.error("Token request failed. Something went wrong!");
      }
    } catch (error) {
      console.error("Error during token request:", error);

      if (error.response && error.response.status === 404) {
        console.error("Invalid verification code or user not found");
      }
    }
  };

字符串
第一个问题:POST 'backend enpoint' 400(Bad Request)
第二个问题:
令牌请求期间出错:AxiosError {message:“请求失败,状态代码为400”,名称:“AxiosError”,代码:“ERR_BAD_REQUEST”,配置:{...},请求:XMLHttpRequest,...}代码:“ERR_BAD_REQUEST”配置:{transitional:{...},适配器:Array(2),transformRequest:Array(1),transformResponse:Array(1),超时:0,...}消息:“请求失败,状态代码为400”名称:“AxiosError”请求:XMLHttpRequest {onreadystatechange:null,readyState:4,timeout:0,withCredentials:false,upload:XMLHttpRequestUpload,...}响应:{data:{...},status:400,statusText:'Bad Request',header:AxiosHeaders,config:{...},...}堆栈:“AxiosError:请求失败,解决时状态代码为400\n(http://localhost:5173/node_modules/. vite/deps/axios. js?v = 08b8f510:1204:12)\n在XMLHttpRequest.onloadend(http://localhost:5173/node_modules/. vite/deps/axios. js?v = 08b8f510:1434:7)”Prototype:错误
我试着自己解决这个问题,但我不能,我也问过chatgpt,但也没有结果,问过blackbox ai,也没有帮助

j2cgzkjk

j2cgzkjk1#

尝试在axios请求中设置withCredentials:true

相关问题