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

352
Vistas
AJAX call fails with SyntaxError: Unexpected end of JSON input

Sorry for making a post with a generic error but I just can't figure this out! I have an ajax call that for now sends an empty object and just returns json_encode(array('status' => 'success')); while I'm debugging. The ajax call is failing with Error in ajax call. Error: SyntaxError: Unexpected end of JSON input

I've tried sending just data['pid']='csv' in case the json needed to have something in it, but still get the same error.

AJAX call

function runDataDownload() {
    var data = {};
  //  data['rawFiles'] = $('#projectIDs').val(); 
  //  data['metadata'] = $('#getData').val();
  //  data['type']=  $('#submitType').val();
  //  data['pid']='csv';
  //  data['command']='data/qcTest';
console.log(data);
console.log(typeof data)
    var qcRunId="csv" + Date.now();
    var posturl = baseURL + "manage/ajax_runBg/csv/" + qcRunId;
    $.ajax({type: "POST", url: posturl, data: data, dataType: 'json'})
            .done(function(result) {
                console.log(result);  

                if (result.status==='success'){ 
                    // begin checking on progress                     
                    checkRunStatus(qcRunId, loopIndex);                   
               }
                else if (result.status==='failed'){ 
                   $('#' + errorId + ' > li').remove();                                                
                   $.each(result.errors, function(key, value) {
                       $('#' + errorId).append( "<li>" + value + "</li>" );
                   });                                                
                   $('#' + statusId).hide();                    
                   $('#' + errorId).show(); 
                }
                else {
                   $('#' + errorId + ' > li').remove();  
                   $('#' + errorId).append( "<li>Invalid return from ajax call</li>" );  
                   $('#' + errorId).show(); 
                   // PTODO - may not be needed
                   // make sure it is visible
                   $('#' + errorId).get(0).scrollIntoView();
               }           
            }) 
                .fail(function(jqXHR, status, err) { 
                    console.log(jqXHR + status + err);
                    $('#' + errorId + ' > li').remove();  
                    $('#' + errorId).append( `<li>Error in ajax call.  Error: ${status} (${err.name}: ${err.message})</li>`);                          
                    $('#' + errorId).show(); 

            });  
}

And my php code:

    public function ajax_runBg($qcName, $runId) {
         echo json_encode(array('status' => 'success'));
    }

Thank you!

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Something seems to clear the PHP output buffer after ajax_runBg has been called. Check this by adding ob_flush(); flush(); to ajax_runBg after the echo statement.

over 4 years ago · Santiago Trujillo Denunciar

0

Sorry for making an answer, when i don't have a full one, I don't have enough reputation to comment.

I ran this code (i removed variables that i don't have) and did not get an error (nothing wrong with "echo json_encode(array('status' => 'success'));").

Here are some possible reasons why it fails: Your problem could be that the php does not echo anything.

I once got this problem and fixed it by first making a variable out of json_encode("stuff to encode") and then doing echo on that variable.

Is there more to the php file that you did not show? There could be a problem if there are other things being echoed.

over 4 years ago · Santiago Trujillo Denunciar

0

When sending json you must first encode it as json, so:

$.ajax({type: "POST", url: posturl, data: JSON.stringify(data), dataType: 'json'})

JSON.stringify

over 4 years ago · Santiago Trujillo 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