[英]Laravel5 AJAX set cookie not working
你能給我建議嗎?
我在 COOKIES 中保存了一些數據。 我在 Laravel 中為此創建了方法,所以我在這個方法上調用 ajax 來設置 cookie,但是當我嘗試調用下一個請求我訪問這個 cookie 值時,它沒有被保存,我可以在第二個請求中訪問它......我不不知道為什么......這很奇怪:/
我有這個代碼:
public function setCookie: method in laravel controller to set cookie
var setCookie: function in javascript where I call ajax request to set cookie
var loadEvents: method called after ajax call where I set cookie.
public function setCookie(Request $request) {
$cookieName = $request->input('cookie_name');
$cookieVal = $request->input('cookie_val');
return response()->json(['status' => 'success'])->withCookie(cookie($cookieName, $cookieVal));
}
var setCookie = function (cookieName, cookieVal) {
$.ajax({
type: 'POST',
url: window.setCookieUrl,
data: {
cookie_name: cookieName,
cookie_val: cookieVal,
_token: getCsrfToken()
}
}).done();
};
public function loadEvents(Request $request) {
$activeCalendarsIds = $request->input('active_calendars_ids');
if($activeCalendarsIds == null)
$activeCalendarsIds = Cookie::get('adminActiveCalendars');
$eventsPage = $this->getPostParam('page');
$eventsLimit = $this->getPostParam('limit');
$this->service->setFilter('page', $eventsPage);
$this->service->setFilter('limit', $eventsLimit);
$events = $this->service->getCalendarsEvents($activeCalendarsIds);
$eventList = view('admin/calendar/event_list', ['events' => $events]);
return response()->json([
'status' => 'success',
'data' => '' . $eventList . ''
]);
}
更新:以下按預期工作。 請求陳述的順序似乎存在混淆。 setCookie ajax 請求必須在發送任何 loadEvent ajax 請求之前完成。
控制器動作:
public function set(Request $request)
{
$cookieName = $request->input('cookie_name');
$cookieVal = $request->input('cookie_val');
return response()->json(['previousCookieValue' => Cookie::get('adminActiveCalendars')])->withCookie(cookie($cookieName, $cookieVal));
}
public function events() {
return response()->json([
'cookieValue' => Cookie::get('adminActiveCalendars'),
]);
}
Javascript/jQuery:
var setCookie = function(cookieName, cookieVal) {
console.log('Set cookie', cookieName, cookieVal);
$.ajax({
type: 'POST',
url: '{{ route('cookies.set') }}',
data: {
cookie_name: cookieName,
cookie_val: cookieVal,
_token: '{{ csrf_token() }}'
},
success: function(response) {
console.log('Response:', response);
}
});
};
var loadEvents = function() {
console.log('Load events');
$.ajax({
type: 'GET',
url: '{{ route('cookies.events') }}',
success: function(response) {
console.log('Response:', response);
}
});
};
模板:
<button onclick="loadEvents();" type="button">Load Events</button>
<button onclick="setCookie('adminActiveCalendars', 'Foo');" type="button">Set Cookie Foo</button>
<button onclick="setCookie('adminActiveCalendars', 'Bar');" type="button">Set Cookie Bar</button>
控制台輸出:
Load events
Object {cookieValue: null} // initially empty
Set cookie adminActiveCalendars Foo
Response: Object {previousCookieValue: null}
Load events
Response: Object {cookieValue: "Foo"} // first loadEvents request after setting cookie already holds correct value
Set cookie adminActiveCalendars Bar
Response: Object {previousCookieValue: "Foo"}
Load events
Response: Object {cookieValue: "Bar"}
完全重新加載頁面后,然后加載事件:
Load events
Response: Object {cookieValue: "Bar"} // still here, right from the start
關於替代方法的說明:
原答案:
這不會設置 cookie:
return view('welcome')->withCookie(cookie($cookieName, $cookieVal, 45000));
這樣做:
$response = new \Illuminate\Http\Response('Test');
$response->withCookie(cookie($cookieName, $cookieVal, 45000));
return $response;
改編:
問題是,如果我們想要訪問我們在前端設置的 cookie,我們會得到 null 作為值。 但是如果我們使用 $_COOKIE 變量,我們可以訪問它,所以這就是 cookie 存在的證據。 那有什么問題呢?
默認情況下,該框架帶有一個用於加密 cookie 的中間件。 如果我們從后端設置一個 cookie,它會自動加密,以便 Laravel 可以讀取它。 從 JS 中我們沒有任何加密,這就是我們無法從框架訪問它們的原因。
在app/Http/Kernel.php中,在web中間件組中,我們可以找到一個EncryptCookies::class
IN array 請設置您使用 jquery 或 javascript 設置的 cookie 名稱
protected $except = [
'cookie-name',
];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.