如何修复 axios post 请求上的错误 422?

时间:2021-07-26 09:11:51

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

我正在尝试使用 axios 请求填充已经构建的 php 数据库,但是我一直收到 422 错误,我不明白我错过了什么。请你帮助我好吗 :) 这是我得到的错误:

xhr.js:177 POST URL/data 422 (Unprocessable Entity)

这是数据库的 Post 请求架构:

"post": {
    "summary": "Post new data row",
    "description": "Post new data row",
    "requestBody": {
      "required": true,
      "content": {
        "application/json": {
          "schema": {
            "type": "object",
            "properties": {
              "data": {
                "type": "string",
                "example": "{\"test\":1}"
              },
              "type": {
                "type": "string",
                "example": "1"
              },
              "status": {
                "type": "integer",
                "example": 1
              }
            },
            "required": [
              "data",
              "type"
            ]
          }
        }
      }
    },
"responses" :{
 "422": {
        "description": "Error",
        "content": {
          "application/json": {
            "schema": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "error": {
                    "type": "string",
                    "example": "Invalid input"
                  }

这是我的代码, 我有几个 switch case,它们都应该以相同的方式工作:

 case "rezervariContact" : {
            const {type, titlu, description, phone, email} = this.state;
            const contactData = {
                type: this.state.type,
                data:{
                    type, 
                    data :{
                        titlu, description, phone, email
                    }
                },
                status:true
            }
            data = {...contactData};
        }
    }
    await axios({
        method: "post",
        headers: { "Content-Type": "application/json",
                    'Accept': '*/*' },
        body:data,
        url : 'http://ms.homens.tricu.ro/data'
      })
        .then(function (response) {
          console.log("RespResolved::",response.config.params);
        })
        .catch(function (response) {
          console.log("catchErrResp::",response);
        });
}

如果你发现了什么,请告诉我。

1 个答案:

答案 0 :(得分:0)

422 可能意味着您输入的数据无效。

原因取决于服务器。您可以尝试参考您尝试访问的 API/服务器的任何文档。