是否可以在Laravel中将数据传递到app.js? 我需要将Session :: getId()的值传递给app.js,因为我在该文件中创建了一个websocket。我知道可以像这样获得CSRF-TOKEN:
$('meta[name="csrf-token"]').attr('content');
但是我需要会话ID。
感谢您的帮助!
答案 0 :(得分:2)
提到的CSRF令牌之所以有效,是因为您的布局中有一个meta标签。
<meta name="csrf-token" content="{{ csrf_token() }}">
不确定公开会话ID的安全性,但是从理论上讲,您可以这样做。
<meta name="session-id" content="{{ session()->getId() }}">
$('meta[name="session-id"]').attr('content');
答案 1 :(得分:0)
您可以从Cookie中获取它。 Laravel中会话ID的cookie名称是laravel_session。
function getCookie(name) {
var matches = document.cookie.match(new RegExp(
"(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
))
return matches ? decodeURIComponent(matches[1]) : undefined
}
var session_id = getCookie('laravel_session');