首页 文章

Laravel 5.5 MDN Fetch API集会话变量不起作用

提问于
浏览
0

我正在使用Laravel 5.5

我试图在GET fetch('/ajax-get-save-session') 请求上设置会话变量 .

Route::get('/ajax-get-save-session', function() { session(['my_key', 'Saved Session']) });

当我尝试在不同的路径中访问会话变量 my_key 时 .

Route::get('/access-session', function() { return session('my_key'); });

返回NULL值 .

这条路线在Laravel web.php 注册,我使用MDN fetch API

任何人?

2 回答

  • 0

    如果我理解你的问题 . 你正在寻找这个 . 试试这个:-

    Use Session; //if your are using laravel 5.2 then add in top in routes. php or laravel 5.2 > greater versions in web.php 
    // now put data in session
    Route::get('/ajax-get-save-session', function() {
     Session::put('my_key','Saved Session');
    });
    //Access the session
    Route::get('/access-session', function() { echo Session::get('my_key') });
    

    希望能帮助到你!

  • 1

    这完全是我的错 . 我从没想过fetch() API是造成我问题的那个 . 它说的描述

    By default, fetch won't send or receive any cookies from the server, resulting in unauthenticated requests if the site relies on maintaining a user session (to send cookies, the credentials init option must be set).
    

    通过做 :

    fetch(url, { credentials : 'include' }).then(response => response.json())
    

    解决了我的问题!

相关问题