Home > Software engineering >  AJAX call fails with SyntaxError: Unexpected end of JSON input
AJAX call fails with SyntaxError: Unexpected end of JSON input

Time:04-06

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!

CodePudding user response:

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.

CodePudding user response:

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.

  • Related