Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

356
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!