你能给我建议吗?
我在 COOKIE 中保存了一些数据。我在 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 . ''
]);
}
更新:以下内容按预期工作。似乎在陈述请求的顺序上存在混淆。在发送任何 loadEvent ajax 请求之前,必须先完成 setCookie 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 就足够 了或者,如果
- 客户端的其他内容不需要此类信息,则将此类信息存储在会话变量中,而不是存储在 Cookie 中。有一些差异。首先,您不必费心处理双方(客户端和服务器(的cookie。
- 或者,不要将此信息存储在任何位置 - 将其作为筛选器参数添加到请求中。
原答案:
这不会设置 cookie:
return view('welcome')->withCookie(cookie($cookieName, $cookieVal, 45000));
这样做:
$response = new 'Illuminate'Http'Response('Test');
$response->withCookie(cookie($cookieName, $cookieVal, 45000));
return $response;
改编:
- https://laravel.com/docs/master/requests#cookies
- https://laracasts.com/discuss/channels/general-discussion/how-to-set-a-cookie-with-laravel-5