Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
142 views
in Technique[技术] by (71.8m points)

javascript - Unexpected end of JSON input ajax

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

question from:https://stackoverflow.com/questions/65891319/unexpected-end-of-json-input-ajax

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...