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

报告相同问题?

悬赏问题

  • ¥50 关于#html5#的问题:H5页面用户手机返回的时候跳转到指定页面例如(语言-javascript)
  • ¥15 无法使用此凭据登录,因为你的域不可用,如何解决?(标签-Windows)
  • ¥15 yolov9的训练时间
  • ¥15 二叉树遍历没有报错但无法正常运行
  • ¥15 在linux系统下vscode运行robocup3d上场球员报错
  • ¥15 Python语言实验
  • ¥15 SAP HANA SQL 增加合计行
  • ¥20 用C#语言解决一个英文打字练习器,有偿
  • ¥15 srs-sip外部服务 webrtc支持H265格式
  • ¥15 在使用abaqus软件中,继承到assembly里的surfaces怎么使用python批量调动