gpt4 book ai didi

javascript - Laravel5 AJAX 设置 cookie 不工作

转载 作者:行者123 更新时间:2023-11-29 18:00:42 26 4
gpt4 key购买 nike

你能给我一些建议吗?

我正在 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 请求之前完成。

Controller 操作:

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 就足够了
  • 或者,如果客户端的其他内容不需要此类信息,则将此类信息存储在 Session 变量中而不是 Cookie 中。 There are some differences .首先,您不必费心处理双方(客户端和服务器)的 cookie。
  • 或者,不要将此信息存储在任何地方 - 将其作为过滤器参数添加到请求中。

原始答案:

这不会设置 cookie:

return view('welcome')->withCookie(cookie($cookieName, $cookieVal, 45000));

这样做:

$response = new \Illuminate\Http\Response('Test');
$response->withCookie(cookie($cookieName, $cookieVal, 45000));
return $response;

改编自:

关于javascript - Laravel5 AJAX 设置 cookie 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35182197/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com