When trying to send an array of objects in JSON format using AJAX, I'm facing an issue where only the token is being received when accessing $request->getContent. It appears that the data is not being sent properly. Can anyone assist me with this problem?
The form in createPage.blade.php
<form action="/invoices/submitInvoice" method="post" id="my-form">
{{ csrf_field() }}
TABLE STRUCTURE CODE GOES HERE
<button type="submit">Send</button>
</form>
<button onclick="myCreateFunction()">Add</button>
Sending logic in sendGood.js
let counter = 0,
nameContainer = [],
priceContainer = [],
obj = {},
objArr = [];
$(function(){
$('#my-form').submit(function(e){
let elements = document.querySelectorAll("#my-form input[type=text]");
// Loop over inputs and gather values
for (let i = 0, element; element = elements[i++];) {
if(element.type === "text"){
if(element.value){
if(element.className === "goodName")
nameContainer.push(element.value);
else
priceContainer.push(element.value);
}
}
}
// Push to object array to be sent via AJAX
for(let i = 0; i < nameContainer.length; i++){
obj.name = nameContainer[i];
obj.price = priceContainer[i];
objArr.push(obj);
obj = {};
}
let route = $('#my-form').data('route');
$.ajax({
type : 'POST',
url : url('/invoices/submitInvoice'),
data : {dataInput : JSON.stringify(objArr)},
contentType: 'application/json; charset=utf-8',
dataType : 'json',
success: function(Response){
console.log("daw",Response);
}
});
e.preventDefault()
});
});
InvoiceControllerWeb.php
public function invoiceCreate(Request $request){
dd($request->all());
}
Result
^ array:1 [▼
"_token" => "hEfjKsDHZadG3G5fCIgEJWcrNrFpv6G4PtjUPzJA"
]