简体   繁体   English

是否可以在Laravel中将数据传递给app.js?

[英]Is it possible to pass data to app.js in Laravel?

Is it possible to pass data to app.js in Laravel? 是否可以在Laravel中将数据传递给app.js? I need to pass the value of Session::getId() to app.js since I create a websocket in that file. 我需要将Session :: getId()的值传递给app.js,因为我在该文件中创建了一个websocket。 I know it is possible to get the CSRF-TOKEN like so: 我知道可以像这样获得CSRF-TOKEN:

$('meta[name="csrf-token"]').attr('content');

but I need the session ID. 但我需要会话ID。

Thanks for any help! 谢谢你的帮助!

The mentioned CSRF token works because you have a meta tag in your layout. 提及的CSRF令牌之所以有效,是因为您的布局中有一个meta标签。

<meta name="csrf-token" content="{{ csrf_token() }}">

Not sure of the security implications of exposing the session id, but you could theoretically do the same. 不确定公开会话ID的安全性,但是从理论上讲您可以这样做。

<meta name="session-id" content="{{ session()->getId() }}">
$('meta[name="session-id"]').attr('content');

You can get it from cookie. 您可以从Cookie获取它。 The cookie name of session id in Laravel it is laravel_session. 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');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM