Codeigniter - REST_SERVER and Datatables (plugin)

Asked

Viewed 15 times

1

I’m having a problem using the library REST_SERVER and the plugin datatables.

The controller is ok, the problem is in Ajax authentication, because if I use the first request Ajax, as below, I can display the data perfectly and work with the table.

But if I use the last code ajax, as documented Datatables, I can’t work like in the first code ajax.

For example, when executing $('#table').ajax.reload(null, false); as if the autenticação header wasn’t working.


Look at my controller

class Api_account extends REST_Controller...
public function getAll_accounts_get()
{
    $draw = intval($this->input->get('draw'));
    $start = intval($this->input->get('start'));
    $length = intval($this->input->get('length'));

    $query = $this->db->get('tb_account');

    $data = [];
    foreach ($query->result() as $r)
    {
        $line = [];
        $type = $r->type;
        $description = [
            1 => 'Conta Corrente',
            2 => 'Poupança',
            3 => 'Outros'
        ];

        $r->default == 1 ? $default = 'Default' : $default = 'No';
        $r->default == 1 ? $class = 'positive' : $class = 'negative';

        $line[] = $r->description;
        $line[] = $description[$type] ? : 'undefined type';
        $line[] = 'R$ '.number_format($r->balance, 2,',','.');
        $line[] = '<div class="'.$class.' rounded-20 inline-block"></div><span>'.' '.ucfirst($default).'</span>';

        $read   = '<button id="'.$r->id.'" class="btn-read">read</button>';
        $update = '<button id="'.$r->id.'" class="btn-update">update</button>';
        $delete = '<button id="'.$r->id.'" class="btn-delete">delete</button>';

        $line[] = [$read.' '.$update.' '.$delete];

        $data[] = $line;
    }

    $result = [
        'draw'            => $draw,
        'recordsTotal'    => $query->num_rows(),
        'recordsFiltered' => $query->num_rows(),
        'data'            => $data
    ];

    $this->response($result, REST_Controller::HTTP_OK);
}

Functional ajax

function getAllAccontsAjax() {
	if (responseToken != null && responseToken != '') {
		let id = jwt_decode(responseToken).id;
		let settings = {
			async: true,
			crossDomain: true,
			url: baseUrl + ci_controller + '/getAll_accounts',
			method: 'GET',
			headers: {
				'authorization': 'Basic YWRtaW46MTIzNA==',
				'Authorizationkeyfortoken': String(responseToken),
				'cache-control': 'no-cache',
				'postman-token': '51901e9b-3613-248b-621e-ffd06d92ded4'
			},
			processData: false,
			contentType: false,
			statusCode: {
				401: function (error) {
					console.log(error);
					location.href = base_url + 'token/logout';
				}
			}
		};
		$.ajax(settings).done(function (response) {
			$('#table').dataTable({
				'scrollY': 390,
				'deferRender': true,
				'scroller': true,
				'scrollCollapse': true,
				'responsive': true,
				'dom': "frtiS",
				'bDestroy': true,
				'data': response.data,
				'columns': [
                                        {'data': 1},
					{'data': 2},
					{'data': 3},
					{'data': 4},
					{'data': 5},
				]
			})
			$('.tooltip').tooltip('hide');
		});

		$.extend($.fn.dataTable.defaults, {
			dom: 'Bfrtip'
		});
	}
}

Look at my ajax (which doesn’t work)

/** Ajax **/
function initDatatable() {
	table = $('#table').DataTable({
		'scrollY': '350px',
		'scrollCollapse': true,
		'processing': false,
		'responsive': true,
		'serverSide': true,
		'dom': "frtiS",
		'deferRender': true,
		'ajax': {
			'url': baseUrl + ci_controller + '/getAll_accounts',
			'type': 'GET',
			'headers': {
				'authorization': 'Basic YWRtaW46MTIzNA==',
				'Authorizationkeyfortoken': String(responseToken),
				'cache-control': 'no-cache',
				'postman-token': '51901e9b-3613-248b-621e-ffd06d92ded4'
			},
		},
	});
	debugger;

	$.extend($.fn.dataTable.defaults, {
		dom: 'Bfrtip'
	});
};

1 answer

0

I don’t know why this happens, but the problem is in the session configuration.

I solved the problem by changing the following file line config.php

BEFORE
$config['sess_expiration'] = -1;

NOW
$config['sess_expiration'] = 7200;

At first setup, I was leaving as -1, because it would prevent the session from expiring, but as I went through this problem, I had to change to 7200

Browser other questions tagged

You are not signed in. Login or sign up in order to post.