0

Here is what is going on. I have an error in my Ajax code that is causing the following error:

Unexpected end of JSON input ajax

Here is my code:

I'm getting data from an array by doing the following:

      echo json_encode($departTickets);

Then I'm parsing the JSON by doing the following:

           $("[data-department-id]").click(function() {                   
                id = $(this).attr('data-department-id');  
                $.ajax({
                    type: 'POST',                       
                    data : {
                      'id' : id  
                    },
                    url:"/desk/template/fetchtickets.php",
                   
                    success: function (res) {                           
                        var data = jQuery.parseJSON(res);
                        
                        for (var jsonId in data) {                               
                            $('#department_'+id).html(jsonId);                                   
                        }   
                    }
                });                                       
            });

Based on the code, what could be causing the issue?

Thank you, Kevin Davis

5
  • 4
    simple debugging, what do you get by console.log(res)? Commented Jan 25, 2021 at 19:41
  • Maybe your JSON is not valid. Try to log response and use validator jsonformatter.curiousconcept.com in order to verify it. Commented Jan 25, 2021 at 19:47
  • I did use the console.log(res) and I'm getting nothing for some reason. Commented Jan 25, 2021 at 19:51
  • @Kevin maybe your id is incorrect... do a simple echo 'works'; and see if your are getting it in console.log(res); Commented Jan 25, 2021 at 19:55
  • I tried it. However, it works, however, when I do the json_encode, nothing is coming back. I even did a print_r as well. Commented Jan 25, 2021 at 20:08

2 Answers 2

1

Number 1 echo json_encode($departTickets); your encoding the data in json.

Then parsing it to AJAX, but you have not told ajax that your dataType is in json.

So we tell ajax like this

       $("[data-department-id]").click(function() {                   
            id = $(this).attr('data-department-id');  
            $.ajax({
                type: 'POST',
                url:"/desk/template/fetchtickets.php",
                dataType: 'json',
                data : {
                  'id' : id  
                },
                success: function (res) {                           
                    var data = jQuery.parseJSON(res);
                    
                    for (var jsonId in data) {                               
                        $('#department_'+id).html(jsonId);                                   
                    }   
                }
            });                                       
        });

Please note how i changed the position of url and placed the dataType bellow it.

Sign up to request clarification or add additional context in comments.

Comments

0

Found a solution..

First here is how I found the resolution.

I used the following command:

      echo json_last_error_msg();

Then it was an encoding issue with the data, so I did the following is:

      $departTickets =  mb_convert_encoding($departTickets, 'UTF-8', 'UTF-8');  
      echo json_encode($departTickets); 

Problem solved.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.