首页 > 解决方案 > 通过使用 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 查看数据,但似乎是正确的。非常感谢您的帮助。

标签: javascriptreactjsformsserveraxios

解决方案


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

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


推荐阅读