I need to submit an array of JSON objects
[{"id":"321","position":"2"},{"id":"359","position":"3"}]
So I tried
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': _token
}
});
table.on( 'row-reorder', function ( e, diff, edit ) {
var myArray = [];
for ( var i=0, ien=diff.length ; i<ien ; i++ ) {
var rowData = table.row( diff[i].node ).data();
myArray.push({
id: rowData.id, // record id from datatable
position: diff[i].newData // new position
});
}
var jsonString = JSON.stringify(myArray);
//jsonString output an array as the one above
$.ajax({
url : "/requests/setOrder.php",
type : 'POST',
data : jsonString,
dataType: 'json',
success : function ( json )
{
$('#example23').DataTable().ajax.reload(); // now refresh datatable
$.each(json, function (key, msg) {
// handle json response
});
}
});
});
In setOrder.php I would like to dump my array so I tried:
$result = json_encode($_POST);
var_dump($result);
The array is correctly submitted, so why the response is empty?
var jsonString = JSON.stringify(myArray);and try again