2014-12-11 70 views
1

我试图根据传入的国家/地区获取城市通过ajax发送。但对于一些国家,我可以得到城市和更新自己的状态与这些城市,针对其他我有这个错误JQuery Ajax错误{“readyState”:0,“responseText”:“”,“status”:0,“statusText”:“OK”}

{"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() 
        } 
       }); 
      }); 
+0

在控制器中删除'foreach'循环*可能*打印出消息。除此之外,我看不到你在格式化json – Justinas 2014-12-11 05:58:11

+0

也尝试使用urlencode()方法对城市名称进行编码,并在js中解码后使用它,以避免在城市名称中出现特殊字符问题。 – PHJCJO 2014-12-11 06:02:16

+0

@Justinas foreach是通过日志查看模型返回的城市的值。我认为在我的控制器I格式中使用“echo json_encode()”并将城市返回为json – Pisix 2014-12-11 06:12:00

回答

0

在没有任何城市的国家出现错误。

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的错误。 我已经在本地测试过,如果没有问题。

0

一切似乎都没什么问题,但在这里,我对你的代码的猜测: ==>确保国家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; 

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)); 
} 

这样:

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)); 
} 

3 - 在你看来,改变这一设置:

var country_id = $('#paysdepart').val(); 

这样:

var country_id = $(this).val(); 

注意:您可以直接访问该页面的“http: // {mywebsite}/get_ville_depart/{country_id}“ {country_id}等于您遇到问题的国家ID,并在使用Ajax查询之前检查一切是否正常。

相关问题