weixin_33695450 2014-01-17 11:03 采纳率: 0%
浏览 35

Ajax返回数据失败

I am using javascript to fetch values for a select option - triggered by a change in previous select option.

        $('#groupid, #lbl_groupid').hide();
        $('#subgroupid, #lbl_subgroupid').hide();

        $('#managerid').change(function(){
            var manager_id = $('#managerid').val();                
            if (manager_id != "")
            {
            var post_url = "nab1/index.php/main_page/get_groups/" + manager_id;

            $.ajax({
              type: "POST",
              url: post_url,
              success: function(group_list) //we're calling the response json array 'cities'
              {
                $('#groupid').empty();
                $('#groupid, #lbl_groupid').show();

                $.each(data,function(id,value) 
                {

                  var opt = $('<option />'); // here we're creating a new select option for each group
                      opt.val(id);
                      opt.text(value);
                      $('#groupid').append(opt); 
            });
           } //end success
     }); //end AJAX
} else {
    $('#groupid').empty();
    $('#groupid, #lbl_groupid').hide();
}//end if
}); //end change 

When i am calling function in the browser get_groups along with a parameter i am getting {"4":"nab_group_1_1","6":"nab_group_2_1"}

however options are not being populated.

I have following code in controller

function get_groups()
{
    $manager = $this->uri->segment(3);

    $group_list = $this->models_personnel->get_groups_by_managerid($manager);
    //header('Content-Type: application/x-json; charset=utf-8');    
    echo  json_encode($group_list);

}  

can somebody please tell whats wrong.

When i am using return from ajax call in an alert its showing me code of the current page. Please tell me how can i effectively implement this so that select option are properly populated. Thanks in advance

  • 写回答

1条回答 默认 最新

  • weixin_33717298 2014-01-17 12:06
    关注

    You have a mistake in the code here:

    $.each(data,function(id,value) 
    

    That should be:

    $.each(group_list,function(id,value)
    

    Because you named the returned data group_list in here:

    success: function(group_list)
    

    Also, add dataType: "json", to your ajax call:

    $.ajax({
        type: "POST",
        url: post_url,
        dataType: "json",
        // etc 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog