[Vuejs]-How should i format my JSON in Vue using Axios to look like this?

0👍

Thanks for your answers, I actually had to make a FormData and populate it with stringified JSON data.

1👍

You can use the JSON.stringify method and append your object to your form data.

bodyFormData.append('data', JSON.stringify(jsonData));

1👍

You are explicitly using Content-Type: multipart/form-data and sending form data for your data parameter. To send JSON, set data to the object you want to serialize as JSON, and use application/json as your content-type.

async register () {
  try {
    const response = await axios({
      method: 'post',
      url: 'backend/index.php?action=createUser',
      data: { firstname: 'Coca', lastname: 'Cola' },
      headers: {'Content-Type': 'application/json' }
    });
    //handle success
    console.log(response);
  } catch (err) {
    //handle error
    console.log(err);
  }
}

Leave a comment