【发布时间】:2018-10-23 12:26:16
【问题描述】:
我如何获取我的代码,以便如果我的 AJAX 按钮单击代码出现 401 错误,那么它要么在单击按钮时引发错误消息并显示“您需要登录”,要么将用户重定向到登录?目前它只是在开发控制台中抛出错误,因此用户不知道按钮为什么不起作用。
Ajax js:
$('.visit').on('click', function (event) {
event.preventDefault();
// Make the button a variable here
var buttonToChange = $(this);
$.ajax({
method: 'POST',
url: urlVisit,
data: {
place_id: $(event.target).data("id"),
_token: token
},
success: function(){
// Change the button here
if(buttonToChange.hasClass('btn-visited')){
buttonToChange.addClass('btn-not-visited');
buttonToChange.removeClass('btn-visited');
buttonToChange.html('Not Visited');
//Count will go down
}
else{
// Do the opposite
buttonToChange.addClass('btn-visited');
buttonToChange.removeClass('btn-not-visited');
buttonToChange.html('Visited');
//count will go up
}
},
});
【问题讨论】: