Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

122
Vistas
How to send Json to PHP with JS

I'm trying to send a Json object to PHP, but when I run the PHP code it doesn't receive anything. I get the Json from transforming an excel file into Json and from there I want to send it to PHP to process it and save it in the database. The problem is that the Network does not send me anything:

below my code:

//this code is to convert excel file in json
let selectedFile;

document.getElementById('importBUDid').addEventListener('change', (event)=>{
    selectedFile = event.target.files[0];
});

document.getElementById('uploadBud').addEventListener('click', ()=>{
    let jsonObj;
    if (selectedFile) {
        let fileReader = new FileReader();
        fileReader.readAsBinaryString(selectedFile);
        fileReader.onload = (event)=>{
            //console.log(event.target.result);
            let dato = event.target.result;
            let workbook = XLSX.read(dato, {type:"binary"});
            //console.log(workbook.Strings);
            workbook.SheetNames.forEach(sheet => {
                let rowObject = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheet]);
                //console.log(rowObject);
                jsonObj = rowObject;
            });
            //console.log(jsonObj);
            testBud(jsonObj);
            
        }
    }
    
}); 
//this code is to send the json object to PHP
function testBud(dt1){
    const dt = JSON.stringify(dt1)
    //console.log(dt);


    const data = new FormData();
    const jsonBlob = new Blob([dt], {type: "application/json"});
    data.append("data", jsonBlob);
    //console.log(data.data);
    console.log(data.data);
    fetch("model/importBud.php", {
    method: "POST",
    body: data.data,
    /* headers:{
      'Content-Type': 'application/json'
        }, */
    }).then(resp => {
    if(!resp.ok) {
        const err = new Error("Response wasn't okay");
        err.resp = resp;
        throw err;
    }
    console.log(resp);
    }).catch(err => {
    console.error(err);
    });
}

PHP below

$obj = $_POST['data'];

if ($obj != "") {
    echo "lleno";
}else{
    echo "vacío";
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Try moving the php file into a Public folder, and then do the fetch call like this:

fetch("./public/model/importBud.php", {method: "POST", [...]);
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda