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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵