javascript - 通过使用 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);
});
}
但是,如果我将数据作为对象“this.props”发送,那么不幸的是我从服务器收到错误 500。这是 this.props 的代码
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
.
推荐阅读
- javascript - 工作流程如何将一个子模块 typescript 写成一个非 typescript 模块?
- c++ - 使用表达式-SFINAE 的函数模板的类定义
- java - 如何通过带有@annotation 的 Open Api 3.0 描述我的对象的模式?
- c - 使用命令行参数时出现分段错误
- c# - Xamarin Forms 宽度按预期表现
- perl - Wxperl 创建小部件但不放置它
- javascript - 如果行被Google表格中的过滤器隐藏或未隐藏,如何获取最后一个可见行?
- javascript - Puppeteer 选择查询 - 如何在 Puppeteer 中选择此查询?
- bash - 如何在不使用期望命令的情况下从 shell 脚本提供密码
- docker - Docker启动有什么意义吗?