E
E
Evgeny Zhurov2019-04-26 16:20:03
JavaScript
Evgeny Zhurov, 2019-04-26 16:20:03

How to load data from OAuth and process in dataTable?

The task is to get data from forms (forms on gravity forms, wordpress, work with them via API), there is a consumer key and a secret key. When using a normal Ajax request, the data arrives, but it is not possible to make friends with dataTables.

Authorization source code:

const oauth = OAuth({
      consumer: {
        key: '[ck_...]',
        secret: '[cs_...]'
      },
      signature_method: 'HMAC-SHA1',
      hash_function(base_string, key) {
        return CryptoJS.HmacSHA1(base_string, key).toString(CryptoJS.enc.Base64);
      }
    });

    const request_data = {
      url: 'https://...path...',
      method: 'GET'
    };

    const token = {
      key: '[key]',
      secret: '[secret]'
    };


Ordinary ajax receiving that works:
$.ajax({
      url: request_data.url,
      type: request_data.method,
      data: oauth.authorize(request_data)
    }).done(function(data) {
      console.log(data);
    });


As part of dataTable initialization (doesn't work):
function getEndpoint() {
      
      $.ajax({
        url: request_data.url,
        type: request_data.method,
        data: oauth.authorize(request_data),
        dataSrc: function(data) { 
         console.log('Test'); // На этом уровне = уже ничего не выводится в консоль
          let entries = data.entries; 
          let modified = [];
          for (var i = 0; i < entries.length; i++) { 
            modified.push({
              'checkbox-cell': '<label class="b-checkbox checkbox"><input type="checkbox" true-value="true" value="false"> <span class="check"></span> <span class="control-label"></span></label>',
              'id_tag': `<a href="javascript:void(0);" class="tablerow_ID" data-id="${entries[key].id}">${entries[key].id}</a>`,
              'created_at_display': `<span class="tablerow_date">${new Intl.DateTimeFormat().format(new Date(entries[key].date_created))}</span>`,
              'created_at_system': entries[key].date_created,
              'name': `<span class="tablerow_name">${entries[key][21.3]} ${entries[key][21.6]}</span>`,
              'address': entries[key][2],
              'city': entries[key][1],
              'status': '<span data-dest="status" class="tag is-light">' + getStatus(entries[key].status) + '</span>',
              'owner': '<span data-dest="handler" class="tag is-custom">' + getHandler(entries[key].handler) + '</span>'
            });
          }
          return modified;
        }
      });
    }

$('#applications').DataTable({
      ajax: getEndpoint(),
      columns: [
        {data: 'checkbox-cell'},
        {data: 'id_tag'},
        {data: {
          _: 'created_at_display',
          sort: 'created_at_system',
        }},
        {data: 'name'},
        {data: 'address'},
        {data: 'city'},
        {data: 'status'},
        {data: 'owner'},
      ],
...
});


What's wrong?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question