@hecate0211 use JSON.stringify in javascript and json_decode in PHP
Sep 14, 2017
11
Level 1
how to send array from script to controler
i want to send my array to controller, but i idont understand how to send it
this is my view
<button class="btn btn-primary" id='save'>Import File</button>
and this is my script
document.querySelector('#save').addEventListener('click', function() {
var dataTables = [];
var rows = hot.countRows()-20;
var cells = hot.countCols();
for(i =1; i<rows;i++){
dataTables = [dataTables, [hot.getDataAtCell(i,0), hot.getDataAtCell(i,1),
hot.getDataAtCell(i,2),hot.getDataAtCell(i,3),hot.getDataAtCell(i,4),
hot.getDataAtCell(i,5)]];
}
});
i want to send my array dataTables to my controler can someone help me?
Level 36
- in view
<script type="text/javascript">
function sendData() {
var data = [
1,
2,
3
];
$.ajax({
url:'/test',
type: 'POST',
dataType:'json',
contentType: 'json',
data: JSON.stringify(data),
contentType: 'application/json; charset=utf-8',
});
}
</script>
- in controller
function saveJson(Illuminate\Http\Request $request)
{
$data = json_decode($request->getContent());
}
- in app\Http\Middleware\VerifyCsfrToken
protected $except = [
'/url-of-saveJson'
];
Please or to participate in this conversation.