TypeScript React axios发起的post请求为何不能发送数据?

  axios.post(this.props.submitUrl, {
            Author: comment.author,
            Text: comment.text,
            Sex: comment.sex,
            Hobby: comment.hobby,
            __RequestVerificationToken: comment.veritoken
        })
            .then(function (response) {
                var id = response.data;
                comment.id = id as number;
                var newComments = theThis.state.comments.concat([comment]);
                theThis.setState({ comments: newComments });
            })
            .catch(function (error) {
                console.log(error);
            });

post 的第二个参数应该就是需要发送到server的data吧,server能接受这个post请求,但是就是数据拿不到。为何?

阅读 10.2k
4 个回答

用chromer看看request header content-type 的值是什么? application/x-www-form-urlencoded 还是application/json,然后后台是用什么框架,express需要 bodyParser 中间件,koa需要koa-body中间件

又是你的问题,上次回答你的问题,后果就是被管理员严重警告,差点把我禁言。

前端没看出问题,那么就是服务端接收data的问题。

不知道你用的什么后端语言,node的话就是

req.body.Author
req.body.Text

有可能是__RequestVerificationToken不对,看看有没有报错。
确认后端API是正常的。

npm install qs

 let qs = require('qs');
  Axios.post('/api/', qs.stringify({
    id:Id
  }))
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题