我有一些AJAX函数,它们返回JSON数据类型的响应。但是,似乎服务器上有一些限制,而在本地运行脚本时没有错误。
AJAX代码:
$.ajax({
url: 'alterajax.php',
method: 'POST',
data: {value},
dataType:'json',
success: function(data){
//at the moment for testing
console.log(data)
}
});
PHP:
$alterquery = $connection->prepare("SELECT tracking_type, tracking_change_date, tracking_effective_date, IFNULL(vessel_fcm_new, ''), IFNULL(vessel_fcm_old, ''), IFNULL(vessel_hull_id_new, ''), IFNULL(vessel_hull_id_old, ''), IFNULL(vessel_flag_new, ''), IFNULL(vessel_flag_old, ''), IFNULL(vessel_name_new, ''), IFNULL(vessel_name_old, ''), IFNULL(vessel_length_new, ''), IFNULL(vessel_length_old, ''), IFNULL(vessel_manufacturer_new, ''), IFNULL(vessel_manufacturer_old, ''), IFNULL(vessel_manufacturer_id_new, ''), IFNULL(vessel_manufacturer_id_old, ''), IFNULL(vessel_year_new, ''), IFNULL(vessel_year_old, ''), IFNULL(vessel_value_new, ''), IFNULL(vessel_value_old, ''), IFNULL(owner_id_new, ''), IFNULL(owner_id_old, ''), IFNULL(owner_street_new, ''), IFNULL(owner_street_old, ''), IFNULL(owner_city_new, ''), IFNULL(owner_city_old, ''), IFNULL(owner_state_new, ''), IFNULL(owner_state_old, ''), IFNULL(owner_zip_new, ''), IFNULL(owner_zip_old, ''), IFNULL(owner_country_new, ''), IFNULL(owner_country_old, ''), IFNULL(loss_payee_id_new, ''), IFNULL(loss_payee_id_old, ''), IFNULL(loss_payee_street_new, ''), IFNULL(loss_payee_street_old, ''), IFNULL(loss_payee_city_new, ''), IFNULL(loss_payee_city_old, ''), IFNULL(loss_payee_state_new, ''), IFNULL(loss_payee_state_old, ''), IFNULL(loss_payee_zip_new, ''), IFNULL(loss_payee_zip_old, ''), IFNULL(base_id_new, ''), IFNULL(base_id_old, ''), IFNULL(policy_id_new, ''), IFNULL(policy_id_old, ''), IFNULL(policy_start_date_new, ''), IFNULL(policy_start_date_old, ''), IFNULL(policy_end_date_new, ''), IFNULL(policy_end_date_old, ''), IFNULL(vessel_fcm, ''), IFNULL(vessel_hull_id, ''), IFNULL(vessel_name, ''), IFNULL(vessel_manufacturer_id, '') FROM tracking WHERE tracking_type = ? ORDER BY tracking_change_date DESC LIMIT 20");
$alterquery->bind_param("i", $trackingtype);
$trackingtype = 333;
$alterquery->execute();
$alterquery->bind_result($tracking_type, $tracking_change_date, $tracking_effective_date, $vessel_fcm_new, $vessel_fcm_old, $vessel_hull_id_new, $vessel_hull_id_old, $vessel_flag_new, $vessel_flag_old, $vessel_name_new, $vessel_name_old, $vessel_length_new, $vessel_length_old, $vessel_manufacturer_new, $vessel_manufacturer_old, $vessel_manufacturer_id_new, $vessel_manufacturer_id_old, $vessel_year_new, $vessel_year_old, $vessel_value_new, $vessel_value_old, $owner_id_new, $owner_id_old, $owner_street_new, $owner_street_old, $owner_city_new, $owner_city_old, $owner_state_new, $owner_state_old, $owner_zip_new, $owner_zip_old, $owner_country_new, $owner_country_old, $loss_payee_id_new, $loss_payee_id_old, $loss_payee_street_new, $loss_payee_street_old, $loss_payee_city_new, $loss_payee_city_old, $loss_payee_state_new, $loss_payee_state_old, $loss_payee_zip_new, $loss_payee_zip_old, $base_id_new, $base_id_old, $policy_id_new, $policy_id_old, $policy_start_date_new, $policy_start_date_old, $policy_end_date_new, $policy_end_date_old, $vessel_fcm, $vessel_hull_id, $vessel_name, $vessel_manufacturer_id);
while ($alterquery->fetch()){
$data[] = array($tracking_type, $tracking_change_date, $tracking_effective_date, $vessel_fcm_new, $vessel_fcm_old, $vessel_hull_id_new, $vessel_hull_id_old, $vessel_flag_new, $vessel_flag_old, $vessel_name_new, $vessel_name_old, $vessel_length_new, $vessel_length_old, $vessel_manufacturer_new, $vessel_manufacturer_old, $vessel_manufacturer_id_new, $vessel_manufacturer_id_old, $vessel_year_new, $vessel_year_old, $vessel_value_new, $vessel_value_old, $owner_id_new, $owner_id_old, $owner_street_new, $owner_street_old, $owner_city_new, $owner_city_old, $owner_state_new, $owner_state_old, $owner_zip_new, $owner_zip_old, $owner_country_new, $owner_country_old, $loss_payee_id_new, $loss_payee_id_old, $loss_payee_street_new, $loss_payee_street_old, $loss_payee_city_new, $loss_payee_city_old, $loss_payee_state_new, $loss_payee_state_old, $loss_payee_zip_new, $loss_payee_zip_old, $base_id_new, $base_id_old, $policy_id_new, $policy_id_old, $policy_start_date_new, $policy_start_date_old, $policy_end_date_new, $policy_end_date_old, $vessel_fcm, $vessel_hull_id, $vessel_name, $vessel_manufacturer_id);
}
echo json_encode($data);
?>
错误:
状态:parsererror
错误:语法错误:JSON输入意外结束
错误:语法错误:JSON.parse错误:位置0意外输入
不确定是什么原因造成的。我还有另外2个与此相似的ajax调用(只是从不同的表中进行选择,但是数据量不多,它们以JSON格式返回)
答案 0 :(得分:0)
while循环之后,这解决了不接收数据的问题。
function utf8_converter($array)
{
array_walk_recursive($array, function(&$item, $key){
if(!mb_detect_encoding($item, 'utf-8', true)){
$item = utf8_encode($item);
}
});
return $array;
}
echo json_encode(utf8_converter($data));
是否可能为服务器设置了特定的编码(utf-8)?