W
W
WebDev2019-07-03 16:50:54
JavaScript
WebDev, 2019-07-03 16:50:54

How does laravel accept an array from js?

Silly question, but I'm confused. There is a script on the client that sends data to the server via ajax.
Sends data via axios, it looks like this:

this.$axios.post('/someurl', {
                    param1:       'value1',
                    param2:       'value2',
                    param3:       ['value3', 'value4', 'value5']
                })

This request goes to the server, where it is received on the Laravel side:
$param1 = request()->input('param1');
$param2 = request()->input('param2');
$param3 = request()->input('param3');

$param3 ends up being an array! What kind of magic and at what stage does it occur? Purely logically, I would send an array as a JSON string from the client, on the server I would accept it and parse it. Why does everything work?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stalker_RED, 2019-07-03
@kirill-93

By default, axios actually sends application/json, not application/x-www-form-urlencoded
You can check what exactly is sent to devtools, for example.
Perhaps Laravel automatically expands all of this into "regular" fields.
UPD: It looks like it is ( tyts ).
In its "raw" form, json was (in version 4.2, for example) available via Request::json->all(), then it was removed from the documentation.
Perhaps in the future (or already?) Will be removed from the code. Too lazy to check.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question