【问题标题】:Posting with axios keeps returning Request failed with status code 422使用 axios 发帖不断返回 Request failed with status code 422
【发布时间】:2020-04-01 22:15:03
【问题描述】:

我的代码直到今天都运行良好,所以基本上这个API 所做的是将默认的gcm-id 发送到服务器并注销用户,但是我从今天早上开始一直收到这个错误,我没有知道它是否与服务器端或客户端有关,所以这是我的代码,我想知道我做错了什么或者我能做些什么来修复它?

const ExitAccount = () => {
      const user = {
            gcm_id: 1
      };
      const options = {
            headers: {
                 'Authorization': this.state.Authorization,
                 'content-type': 'application/json'
                 }
            };
            axios.post('sth', { gcm_id: 1 }, options)
                .then((response) => {
                    this.setState({ loading: false, Authorization: "sorryBuddy", profile: 
                    "dropdownProfile hidden", login: "dropdownLogin show" })
                    localStorage.setItem('api_key', this.state.Authorization);
                    console.log(response)
            });
}

当我打开网络查看是什么问题时,它会返回这个

message: "can't find gcm_id"

【问题讨论】:

  • 代码乍一看似乎可以接受。您没有包含有关您的服务器代码的任何信息,因此很难说。您也没有包含您的 axios 配置。

标签: reactjs axios http-status-code-422


【解决方案1】:

你不妨试试:

axios.post('sth', {data: {gcm_id: 1}, options)

或在标题中添加数据,例如:

const options = {
        headers: {
             'Authorization': this.state.Authorization,
             'content-type': 'application/json'
             },
        data: {gcm_id: 1}
        };

希望对你有帮助

【讨论】:

    【解决方案2】:

    post请求的第二个参数是{ gcm_id: 1 }。我看到您还声明了上面的用户,但您根本没有使用它。您作为参数从客户端发送的内容与后端服务器期望在参数中找到的内容之间可能存在不一致。

    我会先看看那里。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-19
      • 2021-10-18
      • 1970-01-01
      • 1970-01-01
      • 2022-08-19
      • 2022-08-03
      • 2021-07-10
      相关资源
      最近更新 更多