0

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.

2
  • You need to stringify the object before you send it e.g. data: { returnObject: JSON.stringify(returnObject) }. It looks like you're using php, so you should be able to get the data with json_decode($_GET['returnObject'], true). The laravel syntax seems to be Input::get('returnObject'); Commented Jun 9, 2014 at 14:53
  • I have tried that already ofc Commented Jun 9, 2014 at 14:54

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.