dqmnueuu459279 2014-12-15 06:23
浏览 42
已采纳

Codeigniter - 在ajax上成功访问json数据

<label>Group Name</label>
   <select id="group" name="groupName">
     <option value="0">Select Group Name</option>
     <?php foreach ($userGroup as $item) { ?>
     <option value="<?= $item->notificationGroupID ?>"><?= $item->groupName ?></option>
     <?php } ?>
   </select>
<br></br>

This is the dropdown which i am populating from database and when i select any option in the dropdown it fires the following ajax post

jQuery(document).ready(function(){
  $("#group").change(function() {
  var group_ids = {"grp_ids" : $('#group').val()};

    $.ajax({
    type: "POST",
    data: group_ids,
    url: "<?= base_url() ?>pushnotification_group_wise/group_wise_skyid",

    success: function(data){
    console.log(data);

         }
       });
     });
  });

now in the method "group_wise_skyid" inside a controller "pushnotification_group_wise" (as i am using codeigniter) the following codes are executing.

  public function group_wise_skyid()
  {

    if(isset($_POST['grp_ids']))
    {
      $skyid['sky_id'] = json_encode($this->pushnotification_model->get_group_eblskyids($_POST['grp_ids']));

      echo $skyid['sky_id'];
    }

which is calling a model function and returning some data which i am echoing. Now in the success function of the ajax post when i do console.log(data) i get the following in the console

    [{"mapID":"17","notificationGroupId":"3","eblskyId":"eblsky33"},{"mapID":"18","notificationGroupId":"3","eblskyId":"eblsky44"}]

which is in the json format. Now i need to access the data to be specific i need to access the

  notificationGroupId
  eblskyId

this two fields of the json string. Now should i store the whole json string in a variable then access somehow or how can i do that.

Thanks

  • 写回答

4条回答 默认 最新

  • douqiao5552 2014-12-15 09:16
    关注

    Modify your method little bit:

    $this->output
        ->set_content_type("application/json")
        ->set_output( json_encode($this->pushnotification_model->get_group_eblskyids($_POST['grp_ids'])) );
    

    Instead of echo $skyid['sky_id'];

    Now Accept the JSON data only you set the dataType in Ajax Request:

    $.ajax({
        type: "POST",
        data: group_ids,
        url: "<?= base_url() ?>pushnotification_group_wise/group_wise_skyid",
        dataType: 'json', 
        success: function(data){
            $.each(data, function(i, data) {
                console.log('notificationGroupId - ', data.notificationGroupId);
                console.log('eblskyId - ', data.eblskyId);
            });
         }
       });
    

    Handle it according to your need.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥20 sim800c模块 at指令及平台
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计