Retrieving data from PHP by using Ajax

  ajax, javascript, php

I can’t retrieving data from PHP by using Ajax. I want to show user detail by id using ajax. The controller (show_user) working fine and I got this value:

[{"user_id":"1","email":"[email protected]","username":"user"}]

I tried to send the value to ajax function in my views, but the value is undefined. I don’t know where the problem is.

This is my Controller

    function show_user($id)
        $dt = $this->user_model->getUserById($id);
        echo json_encode($dt);
        $this->load->view('show_user', $dt);

This is my Model

    function getUserById($id)
        return $this->db->get_where('user', array('user_id' => $id))->result();

This is my Ajax

<script type="text/javascript">
    var id_show;

    function show_function(id) {
            method: 'POST',
            url: '<?php echo base_url() . 'user/show/' ?>' + id,
            dataType: 'json',
            success: function(resp) {
                if (resp.length > 0) {
                    for (var i = 0; i < resp.length; i++) {
                        id_show = resp[i]['user_id'];

Source: Ask PHP