如何使用axios库在发布请求中发送对象数组

时间:2020-04-13 06:42:24

标签: javascript reactjs axios

我需要使用javascript对象数组将数据插入为多行。

props.email包含一个数组,例如

props.email = [
{email: 'najathi@email.com', name: 'najathi'},
{email: 'naharni@email.com', name: 'naharni'},
]

axios.post('/create-email.php', props.email, {
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        })
            .then(response => {
                console.log(response);
            })
            .catch(error => {
                console.log(error);
            });

1 个答案:

答案 0 :(得分:1)

尝试,

const payload = {
  email:[
          {email: 'najathi@email.com', name: 'najathi'},
          {email: 'naharni@email.com', name: 'naharni'},
      ]
}

axios.post('/create-email.php', payload, {
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
    }
})
.then(response => {
    console.log(response.data);
})
.catch(err => {
    console.log(err);
})