javascript - JQuery Ajax 错误 {"readyState":0 ,"responseText" :"" ,"status":0 ,"statusText" :"OK"}

标签 javascript php jquery ajax codeigniter

我尝试根据作为参数传递并通过ajax发送的国家/地区获取城市。但对于某些国家/地区,我可以获取城市并用这些城市更新我的表格,而对于其他国家/地区,我有此错误

{"readyState":0,"responseText":"","status":0,"statusText":"确定"}

当我查看那些返回错误的国家/地区的日志时,我从数据库中检索到了城市的名称。

我不明白为什么会出现这个错误。 我该如何修复它?

请在下面找到我的代码

型号

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

查看

$('#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()
                    }
                });
            });

最佳答案

在没有任何城市的国家/地区会出现错误。

    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 的错误。 我已经在本地测试了一切是否正常。

关于javascript - JQuery Ajax 错误 {"readyState":0 ,"responseText" :"" ,"status":0 ,"statusText" :"OK"},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27416274/

相关文章:

javascript - 如何停止卸载页面?

javascript - 如何统计文本框中输入的字符数并自动显示在旁边?

javascript - 通过Yii2表单插入数据时获取空值

javascript - Flot Bubbles 插件 - 气泡大小

java - 关于用于创建基于论坛的网站的技术的建议

javascript - Ajax 调用服务器 PHP 有时仅打印回请求

javascript - 如何在 JavaScript 中使用 Observables 来处理 "eachSeries"?

php - 通过 PHP 或 Apache 从服务器端上传 HTTP 文件

php - 将具有多个字段的数组传递给 codeigniter 中的 View

javascript - 尝试在 foreach 循环中将变量传递到对象的方法构造函数时出现范围问题