You will most likely need to replace all HTML inside your <body> tag since you're changing language... anyways, you can use jQuery's load() function for that.
Jul 7, 2016
11
Level 4
How to reload session data via Ajax without reloading the page?
Hello guys, I have a Lang switcher menu that change my App`s lang saving it in the session. But, I want to know if it's possible to refresh all view data without reloading the page..
I'm trying the following in my menu:
$(document).ready(function () {
$('.langs a').on('click', function () {
var lang_prefix = $(this).data('lang');
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
data: {lang: lang_prefix},
type: 'GET',
url: '/lang/'+lang_prefix,
success: function (result) {
sessionData = result.sessionData
console.log(sessionData);
}
});
});
});
And my controller:
public function language(Request $request, $lang)
{
$langs = Config::get('app.locales');
$default_lang = Config::get('app.locale');
if (array_key_exists($lang, $langs)) {
$request->session()->put('locale', $lang);
} else {
$request->session()->put('locale', $default_lang);
}
$request->session()->save();
return response([
'sessionData' => session()->all()
]);
}
My Ajax is changing the Language, but the data in my view just change if I refresh the page..
Level 50
Please or to participate in this conversation.