Laravel中的Javascript重定向模式



当用户喜欢卡片上的故事时,用户会用"保存";按钮我在.js文件中作为savePost执行此操作。但如果用户没有注册,我将重定向为"用户";location.replace(APP_URL+"/login")";。

我想重定向到主页上的模态,而不是/login。我有可能把它作为一个会议或其他事情来做吗

function savePost(t) {
$.ajaxSetup({
headers: {
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content")
}
});
$(this);
$.ajax({
url: APP_URL + "/save_favorite",
type: "POST",
dataType: "json",
data: {
item: t,
_token: $('meta[name="csrf-token"]').attr("content")
},
success: function(e) {
1 == e.bool ? $("#save-icon-" + t).removeClass("text-muted").addClass("icon-filled") : 0 == e.bool && $("#save-icon-" + t).removeClass("icon-filled").addClass("text-muted")
},
error: function(e) {
location.replace(APP_URL + "/login")
}
})
}

您当前的JS:

function savePost(t) {
$.ajaxSetup({
headers: {
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content")
}
});
$(this);
$.ajax({
url: APP_URL + "/save_favorite",
type: "POST",
dataType: "json",
data: {
item: t,
_token: $('meta[name="csrf-token"]').attr("content")
},
success: function(e) {
1 == e.bool ? $("#save-icon-" + t).removeClass("text-muted").addClass("icon-filled") : 0 == e.bool && $("#save-icon-" + t).removeClass("icon-filled").addClass("text-muted")
},
error: function(e) {
location.replace(APP_URL + "/targetPage?showModal=1")
}
})
}

在目标页面末尾添加:

@if(isset($_GET['showModal'])  && $_GET['showModal'] == 1) 
<script type='text/javascript'>
$(window).on('load', function() {
$('#your modal id').modal('show');
});
</script>
@endif

在您的路线/web.php中添加以下内容:

Route::get('/', function () {
if(Auth::guest()){
return view('target.page');
} else {
return redirect('/somewhere');
}
});

最新更新