Reputation: 13
So I have an array containing many JSON objects, This is what it shows in console.log if i output my array:
{"data" :[{"line":"1","band":"X","val":"0"},{"line":"1","band":"A","val":"6461"},{"line":"1","band":"B","val":"6896"},{"line":"1","band":"C","val":"5826"},{"line":"1","band":"D","val":"3704"},{"line":"1","band":"E","val":"2141"},{"line":"1","band":"F","val":"1198"},{"line":"1","band":"G","val":"682"},{"line":"1","band":"H","val":"70"},{"line":"2","band":"X","val":"0"},{"line":"2","band":"A","val":"87"},{"line":"2","band":"B","val":"65"},{"line":"2","band":"C","val":"48"},{"line":"2","band":"D","val":"35"},{"line":"2","band":"E","val":"12"},{"line":"2","band":"F","val":"14"},{"line":"2","band":"G","val":"4"},{"line":"2","band":"H","val":"0"},{"line":"3","band":"X","val":"0"},{"line":"3","band":"A","val":"0"},{"line":"3","band":"B","val":"0"},{"line":"3","band":"C","val":"0"},{"line":"3","band":"D","val":"0"},{"line":"3","band":"E","val":"0"},{"line":"3","band":"F","val":"0"},{"line":"3","band":"G","val":"0"},{"line":"3","band":"H","val":"0"}]}
returnObj is outputted above. This is how i build the array to post back to laravel
$('table tr td input').each(function()
{
Obj =
{
line : $(this).parent().attr('line'),
band : $(this).parent().attr('band'),
val : $(this).val()
}
returnObj.push(Obj);
});
returnObject = '{"data" :' + JSON.stringify(returnObj) +'}';
console.log(returnObject);
This is my ajax on the page:
$.ajax({
type: "POST",
url: "magic/whatif",
data : returnObject
})
.done(function(response)
{
alert(response);
});
There is no problem with my routes or logic, I simply print_r or echo the data that was passed in as the response so i can see what laravel does with it.
$data = Input::all();
print_r($data);
The problem is laravel can not handle the array of objects so it sends back this:
Array ( [undefined] => )
There are many solutions and I had it working when you just pass a single JSON object to laravel but i need to pass an array containing many JSON objects to laravel from the view.
Upvotes: 0
Views: 2800
Reputation: 146191
Change following line:
returnObject = '{"data" :' + JSON.stringify(returnObj) +'}';
To this:
returnObject = { returnObj : returnObj };
You may access that data using:
$data = Input::get('returnObj');
Output (Dump and Die):
dd($data);
Upvotes: 1