I
I
Ilya2014-10-01 15:05:13
JavaScript
Ilya, 2014-10-01 15:05:13

How to process json server response on client?

Hello.

There is a button:

<a href="#" data-toggle="modal" data-target="#editUser" onclick="idUser(<?=$data->id;?>)"><span class="glyphicon glyphicon-pencil"></span></a>


I send id to the server via ajax:
<script>
    function idUser(id) {
      $.ajax({
        type: 'POST',
        url: '/frontend/users_admin/delete',
        data: id,
        success: function(data) {
          console.log(id);
        }
      });

    }
  </script>


The request is processed on the server and an array encoded in json is formed.
public function edit($id)
  {
    if ($data = $this->users_admin_model->get_data_user($id))
    {
      return json_encode($data);
    }
    else
    {
      return FALSE;
    }
  }


Question. How to get this array back on the client and iterate over it?
Thanks for the answer.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Petrov, 2014-10-01
@ArtMavir

You get an array of

$(document).on('click', '[data-toggle="modal"]', function () {
  $.ajax({
    url: '/frontend/users_admin/delete',
    type: 'POST',
    dataType: 'json',
    data: $(this).data('id')
  }).done(function(data) {
    console.log(data, data.length);
    for (var i = 0; i < data.length; i++) {
      console.log(data[i]);
    }
  }).fail(function(error) {
    console.log(error);
  });
});

D
Dmitry Skogorev, 2014-10-01
@EnterSandman

well, json_decode will give you an array

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question