So these three methods are in the Laravel docs for testing if the request is for JSON.
I basically want to return JSON for any request that is requesting JSON or AJAX.
Request::ajax()
Request::isJson()
Request::wantsJson()
Can I use any of these? Do they overlap?
Not all AJAX requests expect a JSON response, so utilizing request()->ajax()
is useful where you want to determine if the request was an XmlHttpRequest
or not, but the response doesn't care about JSON or not.
Not all requests that contain JSON expect a JSON response. so if you don't care about whether or not the response wants JSON back, but want to determine if JSON was sent in the request, then isJson()
is useful for you.
Not all requests that want JSON responses are AJAX driven, so wantsJson
is useful in the case where you want to return JSON
data, but you don't care how the request came to your server.
Based on these descriptions, make assertions and choose the proper functions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With