dslfq06464 2014-12-11 05:54 采纳率: 0%
浏览 1265

JQuery Ajax错误{“readyState”:0,“responseText”:“”,“status”:0,“statusText”:“OK”}

我试着根据被传递为参数并通过AJAX发送的国家来获取城市。但是对于一些国家,我可以使用这些城市来更新我的表单,而对于另一些国家,我会得到这些错误:

{"readyState":0,"responseText":"","status":0,"statusText":"OK"}

当我在日志中查找那些返回错误的国家时,我会从数据库中检索到城市的名称。我不明白为什么会有这个错误,我怎么才能修好它?

下面是我的代码。

Model

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

Controller

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

View

$('#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()
                    }
                });
            });
  • 写回答

2条回答 默认 最新

  • dongpixi2648 2014-12-22 18:13
    关注

    Error in those country which does not have any City.

        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 */
        }
    

    Use direct URL to see the error for the particuler Country ID. I have tested in local everything if fine .

    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效