Php Codeigniter Ajax调用不工作

Php Codeigniter Ajax调用不工作,php,jquery,codeigniter,Php,Jquery,Codeigniter,从我的一个观点来看,我正在使用一个函数进行ajax调用。该函数不起作用。在控制台中,错误消息如下所示: POST http://localhost/jsnci/section/ajax_call_pop_district_data/10 404 (Not Found) XHR finished loading:POST"http://localhost/jsnci/section/ajax_call_pop_district_data/10". <script type="text/j

从我的一个观点来看,我正在使用一个函数进行ajax调用。该函数不起作用。在控制台中,错误消息如下所示:

POST http://localhost/jsnci/section/ajax_call_pop_district_data/10 404 (Not Found)

XHR finished loading:POST"http://localhost/jsnci/section/ajax_call_pop_district_data/10".
<script type="text/javascript">
    function popDistrictData(){
        var dist_id = document.getElementById("ddlDist").value;
        jQuery.ajax({
            type: "POST",
            url: "<?=site_url()?>section/ajax_call_pop_district_data/"+dist_id,
            dataType: 'json',
            success: function(res) {
                if (res)
                {
                    jQuery("div#districtData").html(res.username);
                }
            }
        });
    }
</script>
我正在进行ajax调用,如下所示:

POST http://localhost/jsnci/section/ajax_call_pop_district_data/10 404 (Not Found)

XHR finished loading:POST"http://localhost/jsnci/section/ajax_call_pop_district_data/10".
<script type="text/javascript">
    function popDistrictData(){
        var dist_id = document.getElementById("ddlDist").value;
        jQuery.ajax({
            type: "POST",
            url: "<?=site_url()?>section/ajax_call_pop_district_data/"+dist_id,
            dataType: 'json',
            success: function(res) {
                if (res)
                {
                    jQuery("div#districtData").html(res.username);
                }
            }
        });
    }
</script>
我的型号代码:

public function pop_district_data($did)
{
    $html_dist_data='';

    $sql="SELECT d_name, d_about, d_attraction, d_howtoreach,d_img_nm
              FROM districts
              WHERE d_id= ?";
    $query = $this->db->query($sql, array($did)); //<---Here passing parameter to sql query
    if ($query->num_rows() > 0) {
        foreach ($query->result_array() as $row)
        {
            $html_dist_data.='<strong>ABOUT '.$row['d_name'].'</strong>';
            $html_dist_data.='<p>';
            $html_dist_data.='<a href="#" class="alignright"><img src="'.site_url().'assets/admin/uploads/district_img/'.$row['d_img_nm'].'" alt="Map of '.$row['d_name'].'" class="alignright" /></a>';
            $html_dist_data.= $row['d_about'];
            $html_dist_data.='</p>';
            if($row['d_attraction'] != ''){
                $html_dist_data.='<strong>ATTRACTIONS</strong>';
                $html_dist_data.= $row['d_attraction'];
            }
            if($row['d_howtoreach'] != ''){
                $html_dist_data.='<strong>HOW TO REACH ?</strong>';
                $html_dist_data.= $row['d_howtoreach'];
            }
        }
    }

    return $html_dist_data;
}

请像这样更改控制器代码

public function ajax_call_pop_district_data()
{
   $did = $this->uri->segment(3);   
   $dist_data = $this->District_model->pop_district_data($did);
   echo $dist_data;
}
像这样的脚本

<script type="text/javascript">
    function popDistrictData(){
        var dist_id = document.getElementById("ddlDist").value;
        jQuery.ajax({
            type: "POST",
            url: "<?=site_url()?>index.php/section/ajax_call_pop_district_data/"+dist_id,
            dataType: 'json',
            success: function(res) {
                if (res)
                {
                    jQuery("div#districtData").html(res.username);
                }
            }
        });
    }
</script>

404是一个HTTP错误。这意味着找不到您请求的资源,或者服务器说sowhat's controller code for ajax method?但控制器中有特定的函数您在ajax调用中使用数据类型:“json”,因此将控制器中的代码替换为echo json_encode$dist_data;在成功函数中,var obj=jQuery.parseJSONres;jQuerydivdistrictData.htmlobj.username;在url参数的section/ajax\u call\u pop\u district\u data前面加一个正斜杠,在model函数中返回完整的结果集,在ajax call的success函数中合成html字符串。