Javascript JQuery Ajax错误{“readyState”0、“responseText”:“status”0、“statusText”:“OK”}
我试图根据作为参数传递并通过ajax发送的国家获取城市。但是对于一些国家,我可以获取城市并用这些城市更新我的表格,对于其他国家,我有这个错误Javascript JQuery Ajax错误{“readyState”0、“responseText”:“status”0、“statusText”:“OK”},javascript,php,jquery,ajax,codeigniter,Javascript,Php,Jquery,Ajax,Codeigniter,我试图根据作为参数传递并通过ajax发送的国家获取城市。但是对于一些国家,我可以获取城市并用这些城市更新我的表格,对于其他国家,我有这个错误 {“readyState”:0,“responseText”:“status”:0,“statusText”:“OK”} 当我在日志中查找那些返回错误的国家时,我从数据库中检索到了城市的名称 我不明白为什么会有这样的错误。 我怎样才能修好它 请在下面找到我的代码 型号 function get_ville_depart($country = null){
{“readyState”:0,“responseText”:“status”:0,“statusText”:“OK”}
当我在日志中查找那些返回错误的国家时,我从数据库中检索到了城市的名称
我不明白为什么会有这样的错误。
我怎样才能修好它
请在下面找到我的代码
型号
function get_ville_depart($country = null){
$this->db->select('NUMVILLEDEPART, NOMVILLEDEPART')->from('villedepart');
$this->db->join('pays','pays.NUMPAYS=villedepart.numpays');
$this->db->where('pays.NUMPAYS', $country);
$this->db->order_by("NOMVILLEDEPART","asc");
$query = $this->db->get();
$cities = array();
if($query->result()){
foreach ($query->result() as $city) {
$cities[$city->NUMVILLEDEPART] = $city->NOMVILLEDEPART;
}
return $cities;
}else{
return FALSE;
}
}
控制器
function get_ville_depart($country){
foreach($this->ville_model->get_ville_depart($country) as $ville){
log_message('debug',json_encode($ville));
}
header('Content-Type: application/json; charset=utf-8');
echo json_encode($this->ville_model->get_ville_depart($country));
}
查看
$('#paysdepart').on("change",function(){
$("#villedepart > option").remove();
var country_id = $('#paysdepart').val();
var base_url="<?= site_url('annonce');?>";
$.ajax({
type: "GET",
url: base_url+"/get_ville_depart/"+country_id,
dataType:'json',
success: function(cities)
{
if(!jQuery.isEmptyObject(cities))
{
$("#ifnotvilledepartindatabase").hide();
$("#dynamicvilledepart").show();
$.each(cities,function(NUMVILLEDEPART,NOMVILLEDEPART)
{
var opt = $('<option />');
opt.val(NUMVILLEDEPART);
opt.text(NOMVILLEDEPART);
$('#villedepart').append(opt);
});
}else
{
$("#dynamicvilledepart").hide();
$("#ifnotvilledepartindatabase").show()
}
},
error:function(error)
{
alert("Error "+JSON.stringify(error));
$("#dynamicvilledepart").hide();
$("#ifnotvilledepartindatabase").show()
}
});
});
$('#paysdepart')。关于(“更改”,函数(){
$(“#villedepart>选项”).remove();
var country_id=$('#paysdepart').val();
var base_url=“”;
$.ajax({
键入:“获取”,
url:base\u url+“/get\u ville\u depart/”+country\u id,
数据类型:'json',
成功:功能(城市)
{
如果(!jQuery.isEmptyObject(城市))
{
$(“#ifnotvilledepartinadabase”).hide();
$(“#dynamicville”).show();
$每个(城市、功能(NUMVILLEDEPART、NOMVILLEDEPART)
{
var opt=$('');
选择值(Numville);
选择文本(第二部分);
$('villedepart')。附加(opt);
});
}否则
{
$(“#dynamicvilledeeption”).hide();
$(“#ifnotvilledepartinadabase”).show()
}
},
错误:函数(错误)
{
警报(“错误”+JSON.stringify(错误));
$(“#dynamicvilledeeption”).hide();
$(“#ifnotvilledepartinadabase”).show()
}
});
});
在没有任何城市的国家/地区出现错误
if($query->result()){
foreach ($query->result() as $city) {
$cities[$city->NUMVILLEDEPART] = $city->NOMVILLEDEPART;
}
return $cities;
}else{
return new stdClass; /* Only this line have to change */
}
使用直接URL查看特定国家/地区ID的错误。
如果一切正常,我已经在当地进行了测试 我觉得一切都很好,但我对您的代码有以下猜测: ==>确保国家ID永远不等于NULL 1-在您的模型中,更改此
if($query->result()){
foreach ($query->result() as $city) {
$cities[$city->NUMVILLEDEPART] = $city->NOMVILLEDEPART;
return $cities;
}
else{
return FALSE;
}
为此:
if($result = $query->result_array())
$cities = array_column($result, 'NOMVILLEDEPART', 'NUMVILLEDEPART');
return $cities;
function get_ville_depart($country){
$countries = $this->ville_model->get_ville_depart($country);
return $this->output->set_content_type('application/json')
->set_output(json_encode($countries));
}
var country_id = $(this).val();
2-在控制器中,更改以下内容:
function get_ville_depart($country){
foreach($this->ville_model->get_ville_depart($country) as $ville){
log_message('debug',json_encode($ville));
}
header('Content-Type: application/json; charset=utf-8');
echo json_encode($this->ville_model->get_ville_depart($country));
}
var country_id = $('#paysdepart').val();
为此:
if($result = $query->result_array())
$cities = array_column($result, 'NOMVILLEDEPART', 'NUMVILLEDEPART');
return $cities;
function get_ville_depart($country){
$countries = $this->ville_model->get_ville_depart($country);
return $this->output->set_content_type('application/json')
->set_output(json_encode($countries));
}
var country_id = $(this).val();
3-在您看来,更改此选项:
function get_ville_depart($country){
foreach($this->ville_model->get_ville_depart($country) as $ville){
log_message('debug',json_encode($ville));
}
header('Content-Type: application/json; charset=utf-8');
echo json_encode($this->ville_model->get_ville_depart($country));
}
var country_id = $('#paysdepart').val();
为此:
if($result = $query->result_array())
$cities = array_column($result, 'NOMVILLEDEPART', 'NUMVILLEDEPART');
return $cities;
function get_ville_depart($country){
$countries = $this->ville_model->get_ville_depart($country);
return $this->output->set_content_type('application/json')
->set_output(json_encode($countries));
}
var country_id = $(this).val();
注意:您可以直接访问此页面“http://{mywebsite}/get_ville_depart/{country_id}”
使用{country_id}等于您遇到问题的国家id,并在使用Ajax查询之前检查一切是否正常 在控制器中,删除可能打印消息的foreach循环。除此之外,我看不到任何地方可以格式化这种JSONALO,所以请尝试使用urlencode()方法对城市名称进行编码,并在js中解码后使用它,以避免城市名称中的特殊字符问题。@Justinas foreach用于通过日志查找模型返回的城市值。我想在我的控制器中使用“echo json_encode()”,我将城市格式化并返回为jsonFind,在代码中显示
statusText
,确认在获得json并显示代码时执行此代码。检查此链接