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条)

报告相同问题?

悬赏问题

  • ¥150 寻找王者荣耀开发作者,合作或者解答
  • ¥15 关于cpci总线的几个问题,有点迷糊
  • ¥15 乳腺癌数据集 相关矩阵 特征选择
  • ¥15 我的游戏账号被盗取,请问我该怎么做
  • ¥15 通关usb3.0.push文件,导致usb频繁断连
  • ¥15 有没有能解决微信公众号,只能实时拍照,没有选择相册上传功能,我不懂任何技术,,有没有给我发个软件就能搞定的方法
  • ¥15 Pythontxt文本可视化
  • ¥15 如何基于Ryu环境下使用scapy包进行数据包构造
  • ¥15 springboot国际化
  • ¥15 搭建QEMU环境运行OP-TEE出现错误