我使用Axios.post将数据发送到后端,数据格式出现问题



请帮忙。。。如果我将数据作为数据集发送到后端(如下面的示例中所示(,则服务器会毫无问题地接受数据。

axios.post('http://localhost:8080/repair',
{
description: 'Descriptiony',
registrationNo: '8899',
expire: '2020-12-13',
startDate: '2020-11-29',
status: 'Red',
errorCode: 'e333',
repairDescription: 'Reparatur',
spitzName: 'Gumowa Kaczka'
})
.then((response) => {
console.log(response);
}, (error) => {
console.log(error);
});
}

但是如果我将数据作为对象"发送";这个道具;那么不幸的是,我从服务器得到了一个错误500。这是代码。操作

continue = e => {
e.preventDefault();
console.log(this.props);
axios.post('http://localhost:8080/repair', this.props,
)
.then((response) => {
console.log(response);
console.log(this.props);
}, (error) => {
console.log(error.message);
});
this.props.nextStep();
};

我尝试使用console.log查看数据,但似乎是正确的。非常感谢您的帮助。

Post()的第二个参数(如果是对象(将使用JSON.stringify()自动序列化,并且请求的content-type设置为application/json。请确保端点http://localhost:8080/repair允许在标头中使用此content-type,因为默认值可能是text/json

https://masteringjs.io/tutorials/axios/post

您应该添加您的道具名称:

axios.post('http://localhost:8080/repair', this.props.propsName)

因为props对象有几个默认属性,比如children等,这可能会导致后端出现错误。

非常感谢Dimitri Bret!(我不能发表评论,这就是我在这里说谢谢的原因(。我完全忘记了stringify,我已经读过了。最后,我能够看到我到底在向服务器发送什么,而在这种形式下,后端不会接受它。现在我只需要处理值的值。以下是我发送的内容:{值}:{注册号}:1212,开始日期}:2021-01-05,过期日期:2021-01-15,状态:红色,描述:gggg;,"repairDescription":"gggg","spitzName":"gggg"}}

但它可能是:{"注册号":"1212","开始日期":"2021-01-05","过期":"2021年1月15日","状态":"红色","描述":"gggg","错误代码":"gg",gggg","spitzName":"gggg"}

最新更新