Najathi
Najathi

Reputation: 3015

How to send array of object in post request using axios library

I need to insert data as multiple rows using an array of the javascript object.

props.email is held an array such as

props.email = [
{email: '[email protected]', name: 'najathi'},
{email: '[email protected]', 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);
            });

Upvotes: 3

Views: 10484

Answers (1)

Rahul Sharma
Rahul Sharma

Reputation: 358

Try,

const payload = {
  email:[
          {email: '[email protected]', name: 'najathi'},
          {email: '[email protected]', 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);
})
    

Upvotes: 1

Related Questions