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 .

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab求解平差
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?