[英]getting parsererror from presumed empty json response
EL PROBLEMO EL问题
My JSON returns an associative array that I take elements from and display on the page. 我的JSON返回一个关联数组,我从中获取元素并显示在页面上。 Sometimes one or more of the sub-arrays will be empty, and this is expected behavior.
有时一个或多个子数组将为空,这是预期的行为。 For those I want nothing displayed on the page.
对于那些我不想在页面上显示任何内容。 My problem is that currently anytime one of those sub-arrays is empty, I get a "parsererror".
我的问题是,当前只要这些子数组之一为空,我都会收到“ parsererror”。 In my application, you switch between months, and this AJAX call is made when the dropdown selection is changed to a different month.
在我的应用程序中,您在月份之间进行切换,并且当下拉列表选项更改为其他月份时,将进行此AJAX调用。 On months where all the sub-arrays have values, I dont get an error.
在所有子数组都有值的月份中,我没有收到错误。 I only get it when one or more of the sub-arrays are empty.
我仅在一个或多个子数组为空时得到它。
PHP 的PHP
first I perform the SQL queries, which I'm leaving out to keep this as short as possible, but these are the 3 resulting arrays: 首先,我执行SQL查询,我将其保留为尽可能短,但这是3个结果数组:
$income = $results->fetchall(PDO::FETCH_ASSOC);
$expense = $results->fetchall(PDO::FETCH_ASSOC);
$recurring = $results->fetchall(PDO::FETCH_ASSOC);
Then I want to assign those to an array, which I then json_encode: 然后我想将它们分配给一个数组,然后我将其进行json_encode:
$array = array(
'income' => $income,
'expense' => $expense,
'recurring' => $recurring
);
echo json_encode($array);
AJAX AJAX
This is basically calling the previous php code, then displaying each record from the database in a table. 这基本上是在调用前面的php代码,然后在表中显示数据库中的每个记录。
var request = $.ajax({
type: "POST",
url: 'inc/functions.php',
dataType: "JSON",
data: {action: "display_categories", cur_month:cur_month}
});
request.done(function(response){
$('#income tbody tr, #expense tbody tr').remove();
for(var i = 0; i < response.income.length; i++) {
$('<tr><td id="' + response.income[i].cat_id_PK + '">' + response.income[i].cat_name + '</td><td id="' + response.income[i].cat_id_PK +
'">$' + response.income[i].cat_amount + '</td></tr>').appendTo("#income");
}
for(var i = 0; i < response.expense.length; i++) {
$('<tr><td id="' + response.expense[i].cat_id_PK + '">' + response.expense[i].cat_name + '</td><td id="' + response.expense[i].cat_id_PK + '">$' + response.expense[i].cat_amount + '</td></tr>').appendTo("#expense");
}
for(var i = 0; i < response.recurring.length; i++) {
$('<tr><td id="' + response.recurring[i].cat_id_PK + '">' + response.recurring[i].cat_name + '</td><td id="' + response.recurring[i].cat_id_PK + '">$' + response.recurring[i].cat_amount + '</td></tr>').appendTo("#expense");
}
});
request.fail(function(jqxhr, textStatus){
alert("Request failed: " + textStatus);
});
};
JSON JSON格式
Here's an example JSON response with two empty sub-arrays. 这是带有两个空子数组的JSON响应示例。
{
"income": [],
"expense": [],
"recurring": [
{
"cat_id_PK": 67,
"cat_name": "Grooming",
"cat_amount": "40.00",
"recur_begin": 1,
"recur_end": 12
}
]
}
Count the number of rows you get from sql query result. 计算从sql查询结果中获得的行数。 if they are greater than 0 then send an extra field as
如果它们大于0,则发送一个额外的字段作为
$d['status'] ='OK';
if no record exists then send the error handling as 如果不存在记录,则将错误处理发送为
$d['status'] ='ERR';
so check the status field in the data success 因此,请检查数据成功中的状态字段
dataType: 'json',
data: you data,
success: function( data ) {
if(data.status == "OK"){
alert('success');
} else{
alert('failure');
}
}
});
try like this ,just to test and then use data 尝试这样,只是测试然后使用数据
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.