Answer the question
In order to leave comments, you need to log in
How to send object via formData to axios?
The problem is that I give the file and the object.
And it turns out that I'm sending this:
file: (binary)
text: [object Object]
How can I send an object in JSON format right away?
Turning the object into a string, sending it to the server is not an option, since I receive this object as a string and there is already another problem parsing JSON. parse (obj) produces A cross-origin error was thrown. React doesn't have access to the actual error object in development. It's just string parsing though.
const instance = axios.create({
baseURL: 'https://url/',
headers: {
Authorization: 'Bearer ' + localStorage.getItem('Token')
}
})
export const coursesAPI = {
createTheory(data) {
const formData = new FormData()
formData.append('image', data.image) //File
formData.append('text', data.text) //Object
return instance.post(`endpoint/`, formData).then((data) => {
return data.data;
})
},
}
Answer the question
In order to leave comments, you need to log in
The question is closed. Added formData.append('text', JSON.stringify(data.text))
export const coursesAPI = {
createTheory(data) {
const formData = new FormData()
formData.append('image', data.image) //File
formData.append('text', formData.append('text', JSON.stringify(data.text))) //Object
return instance.post(`endpoint/`, formData).then((data) => {
return data.data;
})
},
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question