0

I would like to do something very simple but I can't make it work, I only want to send via jquery ajax a multi dimensional array to laravel and get the data back.

For example:

var info = JSON.stringify([{'key':'val1'},{'key':'val2'},{'key':'val3'}]);

$.ajax({
type: "POST",
url: "{!! route('ajaxactivityperemployee') !!}",
data: {"mydata":info},
success: function(msg){
    console.log(msg);
}

On the other side, on my controller, I try this:

public function postActivityPerEmployee(Request $request)
{
    $input = $request->all();
    return $input['mydata'][0]['key'];
}

I ve tried various combinations but I always end up with a 500 page error or in the console, I only get [ and nothing else.

1 Answer 1

1

Don't stringify your info because you will send a string.

use this instead :

var info = [{'key':'val1'},{'key':'val2'},{'key':'val3'}];

which mean that you want to send an array of Objects

Sign up to request clarification or add additional context in comments.

5 Comments

Ok, I ve tried it and now if I return $input['mydata'][0], I see in the console object object and if I try to return $input['mydata'][0]['key'], I get an error 500
you can check which error laravel returns to you, for example with chrome browser you can check with dev tools F12 -> Network, you can see your request in red click on it and tell me about the error.
POST localhost/people/public/activityperemployee 500 (Internal Server Error)
When you click on your request you see on the right panel [Headers, Preview etc.] click on preview and send me the result. Example developers.google.com/web/tools/chrome-devtools/profile/…
I gave up and went another way, I encoded every value as a single variable instead of passing an array and it is working fine now. I m sorry I couldn't make it work.

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.