Laravel cookie - 未通过 ajax 请求删除
Laravel cookie - not deleting with ajax request
我的数据库中有一个 "ideas" 的列表。每次用户单击按钮时,都会显示一个新的随机想法(并添加到 cookie 中)。当没有留下任何想法时,按钮中会显示一条消息以重新开始(=删除 cookie 并获得随机想法)。
以上内容目前有效。问题是重新开始(= 删除 cookie 并再次获得随机想法)。
在我的 ajax 通话中我有:
$.ajax({
type: "POST",
url: "/idea",
data: { noideas: $("#noideas").val() },
success: function(response) {
if(response == false)
{
$(".card").fadeOut(function(){
$(this).remove();
$('<div class="card card-primary">' +
'<div class="card-header">' +
'<h3 class="card-title">Einde</h3>' +
'</div>' +
'<div class="card-body">U heeft alle ideeën bekeken.</div>' +
'</div>').appendTo('.content').fadeIn();
});
$("#noideas").val('true');
$("#idea").text("Start opnieuw.");
}
else
{
$(".card").fadeOut(function(){
$(this).remove();
$('<div class="card card-primary">' +
'<div class="card-header">' +
'<h3 class="card-title">' + response.title + '</h3>' +
'</div>' +
'<div class="card-body">' + response.description +
'</div>' +
'</div>').appendTo('.content').fadeIn();
});
}
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(errorThrown);
}
});
在id为noideas的输入框里设置是否没有想法剩余
在我的函数中(在控制器中)我有:
public function idea(Request $request)
{
$noideas = $request->input('noideas');
// cookie
$ideas = Cookie::get('ideas');
$ideas = unserialize($ideas);
$random_idea = Idea::whereNotIn('id', $ideas)->inRandomOrder()->first();
if($random_idea)
{
if(!in_array($random_idea->id,$ideas))
{
$ideas[] = $random_idea->id;
}
}
else
{
$random_idea = false;
}
Cookie::queue('ideas', serialize($ideas));
if($noideas == "true")
{
Cookie::queue(
Cookie::forget('ideas')
);
$ideas = array();
$random_idea = Idea::whereNotIn('id', $ideas)->inRandomOrder()->first();
}
return response()->json($random_idea);
}
问题是 cookie 没有被删除。我做错了什么?
我假设你在获取 random_idea 后忘记设置 cookie,当请求有 "noidea" 时,或者你忘记删除 #noideas
的真实值。
还有:
- 您不需要检查
!in_array($random_idea->id,$ideas)
,因为它已在您的查询 ::whereNotIn('id', $ideas)
. 中实现
- 当请求有 "noideas" 时,你不需要忘记 cookie,因为看起来你需要在得到随机想法后重置(而不是忘记)cookie。
抱歉,让我重写代码:
public function idea(Request $request)
{
$ideas = [];
if(Cookie::has('ideas') && $request->input('noideas') != 'true'){
$ideas = Cookie::get('ideas');
$ideas = unserialize($ideas);
}
$random_idea = Idea::whereNotIn('id', $ideas)->inRandomOrder()->first();
if($random_idea){
$ideas[] = $random_idea->id;
Cookie::queue('ideas', serialize($ideas));
}
return $random_idea; //return json or null if no unique random idea was found
}
如果 $random_idea 不为空,Laravel 会自动将 $random_idea 转换为 json。然后 ajax:
$.ajax({
type: "POST",
url: "/idea",
data: { noideas: $("#noideas").val() },
success: function(response) {
if(response){ //reverse codition logic
$(".card").fadeOut(function(){
$('.cart-title').text(response.title); //I rewrite this part, because you don't need to remove element and render it every time
$('.cart-body').text(response.description);
$(this).fadeIn();
});
$("#noideas").val('false'); //remove true value
} else {
$(".card").fadeOut(function(){
$('.cart-title').text('Einde');
$('.cart-body').text('U heeft alle ideeën bekeken.');
$(this).fadeIn();
});
$("#noideas").val('true');
$("#idea").text("Start opnieuw.");
}
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(errorThrown);
}
});
我的数据库中有一个 "ideas" 的列表。每次用户单击按钮时,都会显示一个新的随机想法(并添加到 cookie 中)。当没有留下任何想法时,按钮中会显示一条消息以重新开始(=删除 cookie 并获得随机想法)。
以上内容目前有效。问题是重新开始(= 删除 cookie 并再次获得随机想法)。
在我的 ajax 通话中我有:
$.ajax({
type: "POST",
url: "/idea",
data: { noideas: $("#noideas").val() },
success: function(response) {
if(response == false)
{
$(".card").fadeOut(function(){
$(this).remove();
$('<div class="card card-primary">' +
'<div class="card-header">' +
'<h3 class="card-title">Einde</h3>' +
'</div>' +
'<div class="card-body">U heeft alle ideeën bekeken.</div>' +
'</div>').appendTo('.content').fadeIn();
});
$("#noideas").val('true');
$("#idea").text("Start opnieuw.");
}
else
{
$(".card").fadeOut(function(){
$(this).remove();
$('<div class="card card-primary">' +
'<div class="card-header">' +
'<h3 class="card-title">' + response.title + '</h3>' +
'</div>' +
'<div class="card-body">' + response.description +
'</div>' +
'</div>').appendTo('.content').fadeIn();
});
}
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(errorThrown);
}
});
在id为noideas的输入框里设置是否没有想法剩余
在我的函数中(在控制器中)我有:
public function idea(Request $request)
{
$noideas = $request->input('noideas');
// cookie
$ideas = Cookie::get('ideas');
$ideas = unserialize($ideas);
$random_idea = Idea::whereNotIn('id', $ideas)->inRandomOrder()->first();
if($random_idea)
{
if(!in_array($random_idea->id,$ideas))
{
$ideas[] = $random_idea->id;
}
}
else
{
$random_idea = false;
}
Cookie::queue('ideas', serialize($ideas));
if($noideas == "true")
{
Cookie::queue(
Cookie::forget('ideas')
);
$ideas = array();
$random_idea = Idea::whereNotIn('id', $ideas)->inRandomOrder()->first();
}
return response()->json($random_idea);
}
问题是 cookie 没有被删除。我做错了什么?
我假设你在获取 random_idea 后忘记设置 cookie,当请求有 "noidea" 时,或者你忘记删除 #noideas
的真实值。
还有:
- 您不需要检查
!in_array($random_idea->id,$ideas)
,因为它已在您的查询::whereNotIn('id', $ideas)
. 中实现
- 当请求有 "noideas" 时,你不需要忘记 cookie,因为看起来你需要在得到随机想法后重置(而不是忘记)cookie。
抱歉,让我重写代码:
public function idea(Request $request)
{
$ideas = [];
if(Cookie::has('ideas') && $request->input('noideas') != 'true'){
$ideas = Cookie::get('ideas');
$ideas = unserialize($ideas);
}
$random_idea = Idea::whereNotIn('id', $ideas)->inRandomOrder()->first();
if($random_idea){
$ideas[] = $random_idea->id;
Cookie::queue('ideas', serialize($ideas));
}
return $random_idea; //return json or null if no unique random idea was found
}
如果 $random_idea 不为空,Laravel 会自动将 $random_idea 转换为 json。然后 ajax:
$.ajax({
type: "POST",
url: "/idea",
data: { noideas: $("#noideas").val() },
success: function(response) {
if(response){ //reverse codition logic
$(".card").fadeOut(function(){
$('.cart-title').text(response.title); //I rewrite this part, because you don't need to remove element and render it every time
$('.cart-body').text(response.description);
$(this).fadeIn();
});
$("#noideas").val('false'); //remove true value
} else {
$(".card").fadeOut(function(){
$('.cart-title').text('Einde');
$('.cart-body').text('U heeft alle ideeën bekeken.');
$(this).fadeIn();
});
$("#noideas").val('true');
$("#idea").text("Start opnieuw.");
}
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(errorThrown);
}
});