Ajax获取索引页而不是值

Ajax获取索引页而不是值,ajax,codeigniter,Ajax,Codeigniter,我正在尝试使用codeigniter 2.1.4中的ajax从各州获取城市。我所面临的问题不是数据,而是它获取整个html索引页。无法解决问题,请帮助。I代码如下: 控制器 城市模型 状态模型 看法 AJAX 在评论中发布的routes.php中,更改 $route['(:any)'] = 'pages/category_list/$1'; 进入 注意,我在代码中注意到两件事是错误的,但我不确定它们是否是原因: 1-不要使用die调用回显或打印数据,仅在出现错误时使用die 2-ajax调用应

我正在尝试使用codeigniter 2.1.4中的ajax从各州获取城市。我所面临的问题不是数据,而是它获取整个html索引页。无法解决问题,请帮助。I代码如下:

控制器 城市模型 状态模型 看法 AJAX
在评论中发布的routes.php中,更改

$route['(:any)'] = 'pages/category_list/$1';
进入

注意,我在代码中注意到两件事是错误的,但我不确定它们是否是原因:

1-不要使用die调用回显或打印数据,仅在出现错误时使用die

2-ajax调用应该是GET not POST:

  $.ajax({   
        url: "pages/get_cities/"+selState,
    async: false,
    type: "GET", 
    success: function(data) {
            //data is the html of the page where the request is made.
    $('#city').html(data);
    },
    error: function(response) {
    //Do Something on Error
    }
     });

试试这样的

var url = "pages/get_cities/" + $(this).attr('value');
 $.ajax({
    url: url,
    async: false,
    type: "GET",
    dataType: "html",
    success: function(data) {
        $('#city').html(data);
    }
})
php代码

public function get_cities($state){
    header('Content-Type: application/x-json; charset=utf-8');
    echo json_encode($this->city_model->get_cities($state));
    return ;
}

哪个视图调用?请尝试echo而不是Die。我是否需要在routes.php中执行任何操作?谢谢您的帮助,但问题仍然存在。我面临的问题是它获取整个html索引页而不是下拉列表。你能发布你的routes.php内容吗?$route['logout']='pages/logout'$路线['sign-up']='pages/sign_-up'$路由['change-password']='pages/change_password'$路由['Forget-password']='pages/forgotPassword'$路线['ad-details/:any']='pages/ad_details/$1'$路由['login']='pages/login'$路线['ad-list/:any']=“页面/ad_列表/$1'$路由[':任意']='页面/类别列表/$1'$路由['default_controller']='pages'$路由['404_覆盖']=;如果我正在将url路径从url:pages/get_cities更改为url:get_cities,则将获取默认控制器html,否则将获取类别列表html页面,但无法获取城市下拉列表。更改此:$route[':any']='pages/category_列表/$1';进入$route['pages/category_list/:any']='pages/category_list/$1';或者你想要什么样的url。。也许category_list/:any并尝试一下,谢谢你的帮助,但问题仍然存在。我面临的问题是它获取整个html索引页面而不是下拉列表。我需要在routes.php中做些什么吗?尝试为函数创建空白视图,然后在该页面上回显json
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
    $(document).ready(function (){
        $('#states-dropdown select').change(function (){
            var selState = $(this).attr('value');
            console.log(selState);
            $.ajax({
                url: "pages/get_cities", 
                async: false, 
                type: "POST", 
                data: "state="+selState, 
                dataType: "html",

                success: function(data) {
                    //data is the html of the page where the request is made.
                    $('#city').html(data);
                }
            })
        });
    });
</script>
$route['(:any)'] = 'pages/category_list/$1';
$route['pages/category_list/(:any)'] = 'pages/category_list/$1';
  $.ajax({   
        url: "pages/get_cities/"+selState,
    async: false,
    type: "GET", 
    success: function(data) {
            //data is the html of the page where the request is made.
    $('#city').html(data);
    },
    error: function(response) {
    //Do Something on Error
    }
     });
var url = "pages/get_cities/" + $(this).attr('value');
 $.ajax({
    url: url,
    async: false,
    type: "GET",
    dataType: "html",
    success: function(data) {
        $('#city').html(data);
    }
})
public function get_cities($state){
    header('Content-Type: application/x-json; charset=utf-8');
    echo json_encode($this->city_model->get_cities($state));
    return ;
}