title: Laravel 5.0 ajax request to save session variable
body: <p>I am attempting to use an ajax request to save a session variable which is used to disable my site's background images.  As of right now, it DOES work if I simply go to the route itself, however, if I run the function through an ajax request, it fails completely and does NOT save the value to session even if it shows it in the <code>dd(Session::all())</code> right after.</p>
  
  <pre><code>&lt;?php namespace App\Http\Controllers;
  
  use App\Http\Controllers\Controller;
  use Session;
  use Request;
  use App;
  use Response;
  
  class SessionVarController extends Controller {
  
      public function backgroundImgsOff()
      {
          if(Request::ajax())
          {
              Session::put(['backgroundImgDisable' =&gt; true]);
              return Response::json(); 
          }
          else
          {
              App::abort(404, 'Page not found.');
          }
      }
  
      public function backgroundImgsOn()
      {
          if(Request::ajax())
          {
              Session::forget(['backgroundImgDisable']);
              return Response::json(); 
          }
          else
          {
              App::abort(404, 'Page not found.');
          }
      }
  
  }
  </code></pre>
  
  <p>Does anyone know why this seems to refuse to actually save the session variable?  I read somewhere that it may have to do with session states, however, I have been unsuccessful in locating so much as a hint to a solution.</p>
  
  <p>EDIT: here is my ajax (keep in mind this is my first attempt at ajax).</p>
  
  <pre><code>function enableBackgroundImages() {
      if (window.XMLHttpRequest) {
          // code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp = new XMLHttpRequest();
      } else {
          // code for IE6, IE5
          xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      xmlhttp.open("GET","{{ action('SessionVarController@backgroundImgsOn') }}",true);
      xmlhttp.send();
  }
  function disableBackgroundImages() {
      if (window.XMLHttpRequest) {
          // code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp = new XMLHttpRequest();
      } else {
          // code for IE6, IE5
          xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      xmlhttp.open("GET","{{ action('SessionVarController@backgroundImgsOff') }}",true);
      xmlhttp.send();
  }
  </code></pre>
  
  <p>Here are the buttons on the page:</p>
  
  <pre><code>&lt;div id="background-on-off" style="display:inline-block;"&gt;
      Background Images: 
      &lt;a href="#" onClick="enableBackgroundImages();"&gt;
          On
      &lt;/a&gt;
      / 
      &lt;a href="#" onClick="disableBackgroundImages();location.reload();"&gt;
          Off
      &lt;/a&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Lastly, here are my routes:</p>
  
  <pre><code>Route::get('background_images_on', 'SessionVarController@backgroundImgsOn');
  Route::get('background_images_off', 'SessionVarController@backgroundImgsOff');
  </code></pre>
  
  <p>Thanks.</p>