如何在React中使用axios发布对象的嵌套数组?

时间:2020-03-16 17:11:21

标签: json reactjs post axios

我正在尝试使用axios在React中“发布”此newClient对象,在它内部我有一个地址数组和一个客户端数组:

constructor(props) {
    super(props);
    this.state = {
        newClient: {
            ruc: '',
            name: '',
            addresses: [],
            contacts: []
        }
    }
}

“地址”只是一个字符串,而“联系人”是一个JSON对象,其中包含更多字段:

    newContact: {
        name: '',
        phone: '',
        email: ''
    }

组件中的方法:

saveClient() {
    ClientService.saveClient(this.state.newClient)
        .then((res) => {
            this.props.history.push({
                pathname: '/clients'
            })
        });
}

使用中的方法

saveClient(client) {
    console.log(client);
    let clientJSON = {
      'name': client.nombre,
      'ruc': client.ruc,
      'addresses': client.addresses,
      'contacts': client.contacts
    }

    return axios.post(CLIENTS_API_BASE_URL, clientJSON, {
        headers: {
          'content-type': 'application/json',
        }
    });
}
在服务器端

(java / spring)我正在获取所有值,但是我也在获取联系人对象或其字段(名称,电话,电子邮件)具有空值。我能做什么?

预先感谢

0 个答案:

没有答案
相关问题