最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

reactjs - Error: Request failed with status code 422 in javaScript - Stack Overflow

programmeradmin1浏览0评论

I am using JS axios. I do not understand this error. error:

Error: Request failed with status code 422
at createError (createError.js:16)
at settle (settle.js:17)
at XMLHttpRequest.handleLoad (xhr.js:62)

This is my code:

const config = {
        headers: { Authorization: `Bearer ${localStorage.getItem('id_token')}`,
        headers: {
            // Overwrite Axios's automatically set Content-Type
            'Content-Type': 'application/json'
          }
     }
    };
    
    const bodyParameters = {
        password:values.newPassword
    };
    
    
    setIsLoading(true)

    //localhost:5000/api/v1/user/updatepassword

    Axios.post('http://127.0.0.1:5000/api/v1/user/updatepassword', 
    bodyParameters,
    config
       
    )

And I am testing with fetch in chrome console. and got this error:

SyntaxError: Unexpected Identifier

I am using JS axios. I do not understand this error. error:

Error: Request failed with status code 422
at createError (createError.js:16)
at settle (settle.js:17)
at XMLHttpRequest.handleLoad (xhr.js:62)

This is my code:

const config = {
        headers: { Authorization: `Bearer ${localStorage.getItem('id_token')}`,
        headers: {
            // Overwrite Axios's automatically set Content-Type
            'Content-Type': 'application/json'
          }
     }
    };
    
    const bodyParameters = {
        password:values.newPassword
    };
    
    
    setIsLoading(true)

    //localhost:5000/api/v1/user/updatepassword

    Axios.post('http://127.0.0.1:5000/api/v1/user/updatepassword', 
    bodyParameters,
    config
       
    )

And I am testing with fetch in chrome console. and got this error:

SyntaxError: Unexpected Identifier
Share Improve this question edited Dec 22, 2020 at 13:24 Manoj 2,0853 gold badges13 silver badges25 bronze badges asked Dec 22, 2020 at 7:38 xander karimixander karimi 4532 gold badges5 silver badges8 bronze badges 3
  • I think this problem is from your backend. – kyun Commented Dec 22, 2020 at 7:40
  • 2 422 is usually about validation problems. are you sure your backend is working as you expect it to work? did you try that on postman or curl?? – HSLM Commented Dec 22, 2020 at 7:40
  • JavaScript objects cannot have repeated keys. Merge your two headers entries – Phil Commented Jul 23, 2023 at 23:15
Add a ment  | 

2 Answers 2

Reset to default 1

I think your error is use 2 headers in config object. You can write it like this:

const config = {
        headers: { 
             Authorization: `Bearer ${localStorage.getItem('id_token')}`,
             'Content-Type': 'application/json'
        }
}

422 status code means that your backend understands your request, but can't process the body (data) you sent.

You can try:

const config = {
  method: 'post',
  url: 'http://127.0.0.1:5000/api/v1/user/updatepassword',
  headers: { 
             Authorization: `Bearer ${localStorage.getItem('id_token')}`,
             'Content-Type': 'application/json'
           },
  body: {
          password:values.newPassword
        }
}
axios(config).then(response => console.log(response)).catch(err => console.error(err))

If you get the same error, check your backend gets all the fields it needs.

One more thing you can do: Try making the request on Postman and when it works you can get the axios code for it directly in the code section.

发布评论

评论列表(0)

  1. 暂无评论