0
我正在使用Yii 1,當我嘗試從服務器通過json_encode檢索省內城市時遇到問題。當我嘗試從數據庫中通過json_encode檢索數據時出現錯誤
當我檢索到30多條記錄時發生錯誤。
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);
}
}
UPDATE
這是我用來獲取城市省份的代碼。我將鍵和值分開,因爲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與城市類似。
看看那是回來的數據,有可能是嵌在裏面,而不是一個錯誤信息你的JSON數據,還是你的數據包含'<?s? JSON沒有限制,除了服務器內存 –
是的,請閱讀更新。它對這個代碼工作正常。但我不知道它爲什麼會給出這個錯誤。唯一不同的是記錄數量 – LTroya
您沒有解決我的評論,我說你可能會遇到內存問題,查看網絡選項卡並查看回來的數據,它可能包含錯誤消息 –