【问题标题】:getting a 400 status when doing a patch request with axios使用 axios 进行补丁请求时获得 400 状态
【发布时间】:2019-01-21 19:34:43
【问题描述】:

我正在构建一个带有 react 的前端,其中一个组件需要接受补丁请求以增加或减少投票。

任何人都可以找出我可能会为此代码获得 400 分的原因吗?

handleIncrementVote = () => {
    const { commentid, articleid } = this.state;

    const vote = {
        inc_votes: this.state.votes
    }

    axios.patch(`https://lloyd- 
   news.herokuapp.com/api/articles/${articleid}/comments/${commentid}`, 
   { 
   vote })
    .then(res => {
        console.log(res);
        this.setState((prevState) => ({
            inc_votes: prevState.votes + 1
        }), () => {
            console.log(this.state.votes)
        })
    }).catch(err => console.log(err))

当我使用邮递员发送请求时,它工作正常。有任何想法吗?我的语法看起来还行吗?

【问题讨论】:

  • 您到底要向邮递员发送什么请求?您的开发工具中的网络面板显示什么?
  • 您是否检查了开发工具中的网络选项卡以确保您的请求与您在 Postman 中发出的请求相匹配?
  • 好的,所以我在变成 400 之前得到 204 无内容
  • 这很可能是您看到的返回 204 的选项请求。您可以发布 patch 请求的正文吗?
  • 在邮递员中,正文是 { "inc_votes": 5 } 正在工作

标签: javascript reactjs httprequest axios


【解决方案1】:

解决了。我的错!我在对象中传递了 this.props 而不是 this.state。谢谢大家的建议!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-07
    • 2020-06-15
    • 1970-01-01
    • 2021-10-13
    • 1970-01-01
    • 2019-09-12
    • 1970-01-01
    • 2021-10-01
    相关资源
    最近更新 更多