admin管理员组文章数量:1327986
Could you give me advice please?
I'm saving in COOKIES some data. I create method for this in Laravel, so I call ajax on this method to set cookie but when I try to call next request where I access this cookie value, it is not saved and I can access it in second request... I don't know why... it's quite strange :/
I have this code:
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 . ''
]);
}
Could you give me advice please?
I'm saving in COOKIES some data. I create method for this in Laravel, so I call ajax on this method to set cookie but when I try to call next request where I access this cookie value, it is not saved and I can access it in second request... I don't know why... it's quite strange :/
I have this code:
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 . ''
]);
}
Share
Improve this question
edited Feb 3, 2016 at 16:23
Jan Kožušník
asked Feb 3, 2016 at 16:17
Jan KožušníkJan Kožušník
6933 gold badges17 silver badges31 bronze badges
5
- Are you sure the AJAX call that sets the cookie has finished before the call that uses the cookie? – Mihai Răducanu Commented Feb 5, 2016 at 20:22
- And if the cookie-setting method is just what you posted, you could just set the cookie from Javascript – Mihai Răducanu Commented Feb 5, 2016 at 20:23
- Yes, it is finished, because I check AJAX call response. Problem is, that JavaScript setting of cookies is plicated to work with Laravel5, because laravel set cookies na database. – Jan Kožušník Commented Feb 6, 2016 at 8:27
- How about $activeCalendarsIds = $request->cookie('adminActiveCalendars'); – Mihai Răducanu Commented Feb 6, 2016 at 13:39
- Nope :/ This doesn't work. – Jan Kožušník Commented Feb 6, 2016 at 20:07
1 Answer
Reset to default 7 +50Update: the following works as expected. It seems there's a mixup in the order the requests are stated. The setCookie ajax request must be finished before any loadEvent ajax request is sent.
Controller actions:
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);
}
});
};
Template:
<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>
Console output:
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"}
After fully reloading the page, then loading events:
Load events
Response: Object {cookieValue: "Bar"} // still here, right from the start
Notes on alternative approaches:
- Without knowing the internals of the application - it seems setting the cookie on the client side would be sufficient
- Alternatively, store this kind of information in a Session variable rather than in a Cookie if it's not needed for something else on the client side. There are some differences. First of all you would not have to bother handling cookies on both sides (client and server).
- Alternatively, do not store this information anywhere - add it to the request as filter parameters.
Original answer:
This doesn't set a cookie:
return view('wele')->withCookie(cookie($cookieName, $cookieVal, 45000));
This does:
$response = new \Illuminate\Http\Response('Test');
$response->withCookie(cookie($cookieName, $cookieVal, 45000));
return $response;
Adaption of:
- https://laravel./docs/master/requests#cookies
- https://laracasts./discuss/channels/general-discussion/how-to-set-a-cookie-with-laravel-5
本文标签: javascriptLaravel5 AJAX set cookie not workingStack Overflow
版权声明:本文标题:javascript - Laravel5 AJAX set cookie not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742249084a2440410.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论