vendredi 28 juillet 2017

Two ajax calls , Session gets the first one but not the second , laravel 5.4

i'm having two ajax posts called at different times and posting to two different urls , the first one works when i print echo session('first');

but the second one prints empty . I did var_dump('Session::all'); and realized that the session does not even get the second variable posted . What's weird is the two ajax complete normally as i print the variables on console upon completion of the post. what can be the issue here ? Code below :

Blade

First select

 <!---Language Section -->
        <select class="user-profile clearfix" name="locale" id="languageswitcher">
                    <option></option>
                    <option value="en">English</option>
                    <option value="zh">中文</option>
                    </select>

    <!--Language Section End -->

Seconde Blade

<div class="form-group">
                  {!!Form::label('Group',trans('app.select_group'))!!}
                  {!! Form::select('group',$groups, null,['class'=>'form-control','id'=>'Group']) !!} 


</div>

Routes

First Route

Route::post('/language-chooser','languageController@ChangeLanguage');

Route::post('/language/',array(
   'before'=>'csrf',
   'as'=>'language-chooser',
   'uses'=>'languageController@ChangeLanguage',
));

Second Route

 Route::post('/groupChooser','setGroupController@index');

 Route::post('/setgroup',array(
    'before'=>'csrf',
    'as'=>'groupChooser',
    'uses'=>'setGroupController@index',
 ));

Middlewares

First Middleware

public function handle($request, Closure $next)
{
     if(Session::has('locale')){



        $locale=session('locale');
    }
    else{
        $locale='en';

    }
    if(Session('group'))
    {
        return view('home');
    }
    App::setLocale($locale);
    return $next($request);
}

Second Middleware

public function handle($request, Closure $next)
{

    if(Session::has('old'))
    {
        echo session('group');
    }


    return $next($request);
}

script

$(document).ready(function(){


$("#languageswitcher").change(function(){


   var locale=$(this).val();
   //alert(111);
   //console.log(111);
  var _token =$("input[name=_token]").val();


   $.ajax({

      url:"/language",
       type:"POST",
       data:{locale:locale,_token:_token},
       dataType:'json',
       success:function(data){

         console.log(data);  
       },
       error: function(ts) {


           //alert(ts.responseText);
       },
       beforeSend:function(){
           console.log(locale);

       },
       complete:function(data){

           window.location.reload(true);

       }
   });

 }) ;



$("#Group").change(function(){

   sessionStorage.clear();
   var group=$(this).val();
   //alert(111);
   //console.log(111);
  var _token =$("input[name=_token]").val();


   $.ajax({

      url:"/setgroup",
       type:"POST",
       data:{group:group,_token:_token},
       dataType:'json',
       success:function(data){


       },
       error: function(ts) {


           //alert(ts.responseText);
      },
       beforeSend:function(){
           console.log(group);

       },
       complete:function(){

          window.location.reload(true);

       }
     });

    }) ;


 });



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire