0

我正在遵循 Next.js 提供的示例repo 来实现 Passport.JS 和下一个连接。

我有一个表单提交处理程序,它在验证后发送电子邮件密码

我正在使用 axios:

import axios from 'axios';

export default function loginSubmit(
  email,
  password,
) {
  const loginData = {
    email,
    password,
  };
  axios
    .post(`/api/signin`, {
      params: loginData,
      withCredentials: true,
      headers: {
        'Content-Type': 'application/json'
      },

    })
    .then(response => {
    
    })
    .catch((error) => {
      if (error.response) {
        console.log("error.response ", error.response);
      } else if (error.request) {
        console.log("error.request ", error.request);
      } else if (error.message) {
        // do something other than the other two
      }
}

我已经进行了研究,并且400 Bad request意味着错误出在客户端上。

一些想法是检查 URL,在终端中刷新 DNS,清除 cookie。但这些都不起作用。

这是 get 返回的错误:

error.response : {
    "data": "Bad Request",
    "status": 400,
    "statusText": "Bad Request",
    "headers": {
        "connection": "keep-alive",
        "content-length": "11",
        "date": "Sat, 16 Oct 2021 18:49:44 GMT",
        "keep-alive": "timeout=5"
    },
    "config": {
        "url": "/api/signin",
        "method": "post",
        "data": "{\"params\":{\"email\":\"xxxxxx@gmail.com\",\"password\":\"Aug8162016!\"},\"withCredentials\":true,\"headers\":{\"Content-Type\":\"application/json\"},\"credentials\":\"same-origin\"}",
        "headers": {
            "Accept": "application/json, text/plain, */*",
            "Content-Type": "application/json"
        },
        "transformRequest": [
            null
        ],
        "transformResponse": [
            null
        ],
        "timeout": 0,
        "xsrfCookieName": "XSRF-TOKEN",
        "xsrfHeaderName": "X-XSRF-TOKEN",
        "maxContentLength": -1,
        "maxBodyLength": -1,
        "transitional": {
            "silentJSONParsing": true,
            "forcedJSONParsing": true,
            "clarifyTimeoutError": false
        }
    },
    "request": {}
}

我的直觉告诉我这里有东西。

有什么想法可以让我得到更严重的错误吗?

4

2 回答 2

1

我认为您以错误的方式生成了 POST 请求正文。尝试

const data = {
    email,
    password,
  };

  axios
    .post(`/api/signin`, // url
      data, // request body
      { // options
        withCredentials: true,
        headers: {
         'Content-Type': 'application/json'
      }
    })

如果它不起作用,您可以尝试发送已经字符串化的正文:

const data = {
    email,
    password,
  };

  axios
    .post(`/api/signin`, // url
      JSON.stringify(data), // request body as string
      { // options
        withCredentials: true,
        headers: {
         'Content-Type': 'application/json'
      }
    })

它应该工作

于 2021-10-16T19:15:21.797 回答
1

axios.post接受 3 个参数

1-网址

2- 身体

3-选项

axios
    .post(`/api/signin`, 
      data, 
      {
      withCredentials: true,
      headers: {
        'Content-Type': 'application/json'
      },

    })
于 2021-10-16T19:48:04.393 回答