I'm trying to display data from Ajax source, using this HTML:
<table class="table-striped data-table" data-src="https://my.site/json-url" data-columns='["id","sku","name","base_stock","stock","sales"]'>
<thead>
<tr>
<th>ID</th>
<th>SKU</th>
<th>Name</th>
<th>Initial Stock</th>
<th>Current Stock</th>
<th>Sales</th>
</tr>
</thead>
</table>
And this JS:
$('.data-table').each(function(){
var src = $(this).data('src');
var cols = $(this).data('columns').map(x => {return {data: x};});
$(this).DataTable({
"responsive": true,
"ajax": src,
"columns": cols
});
});
Where, https://my.site/json-url normally responds with:
{
"status": "success",
"data": [
{
"id": 1515,
"sku": "",
"name": "Test Simple",
"base_stock": 10,
"stock": 10,
"sales": 0
},
{
"id": 1512,
"sku": "BWTEST",
"name": "Test Variable - Black / White",
"base_stock": 9,
"stock": 9,
"sales": 0
},
{
"id": 1513,
"sku": "CLRTEST",
"name": "Test Variable - Multi-Color",
"base_stock": 4,
"stock": 4,
"sales": 0
}
]
}
However, I'm getting the error: Uncaught TypeError: cannot use 'in' operator to search for "length" in "id" at jquery.min.js:2:1065. Could someone help with this?
NOTE: I can see that no ajax request is actually made to https://my.site/json-url so the error appears to be happening even prior to ajax call.