I'm trying to send data, including a file (it's an image), but when testing what ajax sends, it prints the keys to an empty array ({}), I'm using nodejs with jswebtokenizer, here's my code:
function publicar(){
var nombre_archivo = document.getElementById('imagen').files(0).name;
var file = document.getElementById('imagen').files(0)
var descripcion = document.getElementById("desc").value
var id = localStorage.getItem("Id")
var categoria = document.getElementById("category").innerHTML
let formdata = new FormData()
formdata.append('imagen',file)
formdata.append('nombre_arch',nombre)
formdata.append('descripcion',descripcion)
formdata.append('categoria',categoria)
formdata.append('id',id)
var xmlhttp = new XMLHttpRequest()
xmlhttp.open('POST',url+"/informacion",true)
xmlhttp.setRequestHeader('Content-Type', 'multipart/form-data');
xmlhttp.setRequestHeader('Authorization', 'Bearer ' + localStorage.getItem("Id"));
xmlhttp.send(JSON.stringify(formdata))
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
alert("se subio correctamente")
}
}
I print the result on the server
principal.post("/informacion",(req,res)=>{
console.log(req.body)
})