【问题标题】:How to fix error 422 on axios post request?如何修复 axios 发布请求上的错误 422?
【发布时间】:2021-10-02 05:42:27
【问题描述】:

我正在尝试使用 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://xxxxxxxxx.ro/data'
      })
        .then(function (response) {
          console.log("RespResolved::",response.config.params);
        })
        .catch(function (response) {
          console.log("catchErrResp::",response);
        });
}

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

【问题讨论】:

  • 检查响应正文

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


【解决方案1】:

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

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

【讨论】:

  • MDN解释错误是这样的:422 Unprocessable Entity response status code表示服务器理解请求实体的内容类型,请求实体的语法是正确的,但是无法处理包含的指令。
  • @AnadDana 在 Laravel 中,当您验证数据并且出现问题时,它会发回 422 响应。所以我想可能是因为 JSON 响应有类似 ` "error": { "type": "string", "example": "Invalid input" }`
  • 嗯,知道我的数据有什么无效的地方吗? :)我真的看不出这里的问题:(
  • 如果真的是 Laravel,你检查过你的验证方法了吗?也许问题就在那里
  • 我不知道该怎么做 ?
【解决方案2】:

检查http://xxxxxxxxxx.ro/data上的文档!这意味着您的帖子数据有效但服务器无法正确处理!

【讨论】:

    猜你喜欢
    • 2020-11-19
    • 2019-02-25
    • 2020-05-12
    • 1970-01-01
    • 2018-08-14
    • 1970-01-01
    • 2021-03-31
    • 2020-07-26
    • 1970-01-01
    相关资源
    最近更新 更多