I am trying to send an array of data from client-side using AJAX to the backend Laravel controller. The problem is that I keep getting null when data-dump in the controller.
I have tried many possible solutions online and nothing is working for me. I need your help to know where I have gone wrong or missed. Thanks in advance.
This is the route
Route::post('/event/going/{id}', ['as' => $s . 'going-post', 'uses' => 'PagesController@goingForEvent']);
This is the controller
public function goingForEvent(Request $request, $id)
{
dd(Input::get('result'));
}
This is the HTML code
<a data-url="{{route('public.going-post', $singleEvent->id)}}" id="imGoing" class="ui attached positive button">I'm Going</a>
<div class="or"></div>
This is the AJAX code
<script>
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$("#imGoing").click(function() {
var url = $(this).data('url');
console.log(url);
//var x = url.substr(url.lastIndexOf('/') + 1);
// console.log("/event/going/" + x);
swal({
title: 'Multiple inputs',
html:
'<input type="text" id="firstName" class="swal2-input">' +
'<input type="text" id="lastName" class="swal2-input">' +
'<input type="email" id="email" class="swal2-input">',
preConfirm: function () {
return new Promise(function (resolve) {
resolve([
$('#firstName').val(),
$('#lastName').val(),
$('#email').val(),
])
})
},
onOpen: function () {
$('#firstName').focus()
}
}).then(function (result) {
var stuff ={fn:result[0],ln:result[1], em: result[2]};
console.log(stuff);
$.ajax({
url: url,
type: "POST",
data: {result:JSON.stringify(stuff)},
processData: false, // tell jQuery not to process the data
contentType: false // tell jQuery not to set contentType
}).done(function( data ) {
//console.log(data);
});
}).catch(swal.noop)
});
</script>
Result after using $request->all() at the controller