Send data back and forth as JSON #264
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
tl;dr the "data" of your component is now sent via JSON for the Ajax calls. Ajax calls are still made via
GET
(unless you're triggering an action or your data is too long) with a new?data={... json}
.Longer Explanation
Until now, I've been trying to send data back to the server as
?query
params or as POST data (which is just query params stored in the body of the request).However, using query parameters simply isn't robust enough. Most notably, it makes handling
null
values impossible. If you have a LiveProp - e.g.firstName
- on the server set tonull
, how should that be sent back to the server? As?firstName=
? In that case, how do you distinguish from an empty string? One option is to NOT sendfirstName
at all on the Ajax request. But then, what if you have aLiveProp
that is an array with['firstName' => null]
? If we don't sendfirstName
, the array will dehydrate to an empty array.In the end, the goal is to effectively "freeze" your component's data, send it to the frontend, and have the frontend send it back. Using query parameters simply isn't robust enough to do this without, but JSON is.
There is no real downside to this: it just felt more pure to send data back and forth as query parameters and return HTML. We still return HTML, but sending JSON will make the behavior solid.