简体   繁体   English

JSON 输入意外结束 ajax

[英]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:我的 Ajax 代码中有一个错误,导致以下错误:

Unexpected end of JSON input ajax JSON 输入意外结束 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:然后我通过执行以下操作来解析 JSON:

           $("[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谢谢你,凯文戴维斯

Number 1 echo json_encode($departTickets); 1 号echo json_encode($departTickets); your encoding the data in json.您在 json 中编码数据。

Then parsing it to AJAX, but you have not told ajax that your dataType is in json.然后解析为AJAX,但是你没有告诉ajax你的dataType在json。

So we tell ajax like this所以我们这样告诉 ajax

       $("[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.请注意我如何更改 url 的 position 并将 dataType 放在它下面。

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.问题解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM