当我尝试通过 json_encode 从数据库中检索数据时出现错误
I got an error when I try to retrieve data from the database via json_encode
我正在使用 Yii 1,当我尝试通过 json_encode 从服务器检索该省的城市时遇到问题。
当我检索超过 30k 条记录时发生错误。
Unknown error 200 . textStatus: parsererror. errorThrown SyntaxError: Unexpected token <"
另一个用例城市的省工作正常。
JSON 能装多少有限制吗?
//PHP Code
public function actionGetDinamicCities() {
$code = $_POST['code_province'];
// Busqueda de Registros dependiendo el codigo de Ciudad recibido.
$cities = City::model()->findAll(array('order'=>'city_name',
'condition'=>'state_code=:code_province',
'params'=>array(':code_province'=>$code)
));
// Creacion de un arreglo clave=>valor para un modelo dado
$citiesList = generateList($cities, 'id', 'city_name');
echo json_encode($citiesList);
}
//JQuery Code
$('#Address_id_province').change(function() {
var data = { "code_province" : $(this).val() };
$.ajax({
url: 'GetDinamicCities',
data: data,
type: "post",
dataType: "json",
success: function (data) {
var options = '<option value>Select a City</option>';
console.log(data);
},
error: function (jqXHR, textStatus, errorThrown) {
getError(jqXHR.status, textStatus, errorThrown);
}
});
function getError(status, textStatus, errorThrown) {
switch (status) {
case 404:
console.log('status: File not found. textStatus: ' + textStatus + '. errorThrown: ' + errorThrown);
case 500:
console.log('Status: Server Error. textStatus: ' + textStatus + '. errorThrown: ' + errorThrown);
case 0:
console.log('status: Request aborted. textStatus: ' + textStatus + '. errorThrown: ' + errorThrown);
default:
console.log('Unknown error ' + status+ ' . textStatus: ' + textStatus + '. errorThrown ' + errorThrown);
}
}
更新
这是我用来获取城市省份的代码。我将键和值分开,因为 Chrome 并且 IE 按键对它进行排序,而我希望按值对它进行排序。
$code = $_POST['code_country'];
$states = States::model()->findAll(array('order'=>'state_name',
'condition'=>'country_code=:code',
'params'=>array(':code'=>$code)
));
$statesList = generateList($states, 'state_code', 'state_name');
$statesListSorted = array();
$statesListSorted['k'] = array_keys($statesList);
$statesListSorted['v'] = array_values($statesList);
$json = json_encode($statesListSorted);
echo $json;
处理来自服务器的答案的javascript与城市类似。
您 运行 内存不足。两条建议:
- 推荐: Search/page数据代替。尝试显示 30K 条记录很多,用户通常不希望一次看到那么多记录
- 不鼓励:更改您的内存设置请参阅http://www.ducea.com/2008/02/14/increase-php-memory-limit/如果您在托管环境中,这可能会被允许也可能不会被允许
- php.ini:
memory_limit = 32M
- 以编程方式:
ini_set('memory_limit', '64M');
我正在使用 Yii 1,当我尝试通过 json_encode 从服务器检索该省的城市时遇到问题。
当我检索超过 30k 条记录时发生错误。
Unknown error 200 . textStatus: parsererror. errorThrown SyntaxError: Unexpected token <"
另一个用例城市的省工作正常。
JSON 能装多少有限制吗?
//PHP Code
public function actionGetDinamicCities() {
$code = $_POST['code_province'];
// Busqueda de Registros dependiendo el codigo de Ciudad recibido.
$cities = City::model()->findAll(array('order'=>'city_name',
'condition'=>'state_code=:code_province',
'params'=>array(':code_province'=>$code)
));
// Creacion de un arreglo clave=>valor para un modelo dado
$citiesList = generateList($cities, 'id', 'city_name');
echo json_encode($citiesList);
}
//JQuery Code
$('#Address_id_province').change(function() {
var data = { "code_province" : $(this).val() };
$.ajax({
url: 'GetDinamicCities',
data: data,
type: "post",
dataType: "json",
success: function (data) {
var options = '<option value>Select a City</option>';
console.log(data);
},
error: function (jqXHR, textStatus, errorThrown) {
getError(jqXHR.status, textStatus, errorThrown);
}
});
function getError(status, textStatus, errorThrown) {
switch (status) {
case 404:
console.log('status: File not found. textStatus: ' + textStatus + '. errorThrown: ' + errorThrown);
case 500:
console.log('Status: Server Error. textStatus: ' + textStatus + '. errorThrown: ' + errorThrown);
case 0:
console.log('status: Request aborted. textStatus: ' + textStatus + '. errorThrown: ' + errorThrown);
default:
console.log('Unknown error ' + status+ ' . textStatus: ' + textStatus + '. errorThrown ' + errorThrown);
}
}
更新
这是我用来获取城市省份的代码。我将键和值分开,因为 Chrome 并且 IE 按键对它进行排序,而我希望按值对它进行排序。
$code = $_POST['code_country'];
$states = States::model()->findAll(array('order'=>'state_name',
'condition'=>'country_code=:code',
'params'=>array(':code'=>$code)
));
$statesList = generateList($states, 'state_code', 'state_name');
$statesListSorted = array();
$statesListSorted['k'] = array_keys($statesList);
$statesListSorted['v'] = array_values($statesList);
$json = json_encode($statesListSorted);
echo $json;
处理来自服务器的答案的javascript与城市类似。
您 运行 内存不足。两条建议:
- 推荐: Search/page数据代替。尝试显示 30K 条记录很多,用户通常不希望一次看到那么多记录
- 不鼓励:更改您的内存设置请参阅http://www.ducea.com/2008/02/14/increase-php-memory-limit/如果您在托管环境中,这可能会被允许也可能不会被允许
- php.ini:
memory_limit = 32M
- 以编程方式:
ini_set('memory_limit', '64M');
- php.ini: