skip to Main Content

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!

5

Answers


  1. Chosen as BEST ANSWER

    Making my comment an answer in case someone else runs into this-

    The reason the code was working in my controller was that my colleague's controller had authentication checks in the constructor! So there must have been an authentication error returned, that was not JSON formatted, hence the error..


  2. 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.

    Login or Signup to reply.
  3. 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.

    Login or Signup to reply.
  4. When sending json you must first encode it as json, so:

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

    JSON.stringify

    Login or Signup to reply.
  5. If i remember right, than you have to specify the key and the value in data attr. .

    var data = {};
     data['rawFiles'] =$('#projectIDs').val(); 
     data['metadata'] = $('#getData').val();
      data['type']=  $('#submitType').val();
      data['pid']='csv';
       data['command']='data/qcTest'
    

    … Ajax…
    Data: {dataKey: data}
    ….
    And in the API you can catch it with dataKey name.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search