lundi 4 septembre 2017

Disable Caching in Laravel Ajax View Response

I am currently using Laravel 5.3 and have a view whereby once a user navigates to that page. I do an AJAX request to a controller function which returns a rendered view which is then set in my JavaScript like so:

    function loadUserDetails (selectedDetails)
    {
            $('#user-details-container').html("");

            var url = '';

            $.ajax({
                type: "POST",
                url: url,
                data: {
                    details: selectedDetails,
                },
                success: function(data) {
                    var loadedHtml = data.html;
                    $('#user-details-container').html(loadedHtml);
                }
            });
    }

However then in my Laravel controller where I getUserDetails and actually render my view. I am setting my view up pretty normally however here is how I am trying to return my rendered view as a JSON response.

$returnHTML = view('user-details')->with('userDetails', $userOverview)->render();
    $headers = ['Cache-Control' => 'no-cache, no-store, must-revalidate'];
    return response()->json(array('success' => true, 'html'=>$returnHTML), 200, $headers);

I am aiming to remove server side caching for this view as the user can do this request again with different parameters, I tried using header() but that didn't work...

  //set headers to NOT cache a page
    header("Cache-Control: no-cache, must-revalidate"); //HTTP 1.1
    header("Pragma: no-cache"); //HTTP 1.0
    header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past

I am just wondering how would I remove the server side caching that Laravel is implementing on this route? As the user needs to be able to filter user details and the response must be different and not a cached response.

Any help is appreciated.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire