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
Answer
Solution:
Number 1echo json_encode($departTickets);
your encoding the data in json.
Then parsing it to AJAX, but you have not told ajax that yourdataType
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.
Answer
Solution:
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.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: object not found by the @paramconverter annotation.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.