3

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);
            });
2
  • user JSON.stringify(), to convert your array of objects to a string, then send data to your backend. Commented Apr 13, 2020 at 6:49
  • Error occurred.. POST https://../create-email.php 400 Error: Request failed with status code 400 Commented Apr 13, 2020 at 7:26

1 Answer 1

1

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);
})
    

Sign up to request clarification or add additional context in comments.

6 Comments

axios.post() returns a response object. To get data from response you need write the next code .then(response => { console.log(response.data); })
Error occurred.. POST https://../create-email.php 400 Error: Request failed with status code 400
Try adding the header for 'Content-Type'. I have updated the answer. :)
this is not supported 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
it only supports 'Content-Type': 'application/x-www-form-urlencoded'
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.