今していること
学校システムを構築中、その中で先生を予約する機能をajaxで行う実装を完了(laravel5.5, vue.js/axios)
実現したいこと
(現在)
AJAX通信後、loginのsession情報が切れていた場合
→そのまま直でloginページに移動する
(希望)
「申し訳ございません、もう一度loginし直してください」
などのsweet alert表示後に、login画面に移動する
試してみたこと
//こちらがAJAX通信先APIのconstruct関数 public function __construct() { $this->middleware('auth') } //これを下記のようにしてみる public function __construct() { if($this->middleware('auth') == false){ return response()->json([ 'result' => false, 'comment' => "Login again please" ]); } } //JS側のコード //しかし上記のlaravelコードでは返り値をエラーとして読み込んでしまう axios.post('/ajax/reservation_calender/reserve', data) .then(function(res){ if(res.data.result == false){ // self.rows = res.data.rows; swal({ //↓この内容を表示後、login画面に映りたい title: res.data.comment, text: "You couldn't reserve" +"\n", icon: "warning", }); }else{ swal({ title: "Reserved!", text: "teacher : " + res.data.lesson_info[0].teacher.name +"\n" + "date : " + res.data.lesson_info[0].date + "\n" + "time : " + res.data.lesson_info[0].time.time, icon: "success", }); } }).catch(function (error) { swal({ //**I think its better to send email to me(kazu) here title: "Sorry, you couldn't reserve", text: "Try again please" +"\n", icon: "warning", }); })
よろしくお願いいたします。
回答を受けての追記
「返り値をエラーとして」の意味といたしましては、
.then(function(res)内で処理したいが、login session情報が切れていた場合.catch(function (error)で処理してしまう。ということです
答えのコードを記入
.catch(function (error) { if(error.response.status === 401){ swal({ //**I think its better to send email to me(kazu) here title: "Login again please", text: "your login data is expired" +"\n", icon: "warning", }); window.location.href = "{{URL::to('login')}}" }
回答1件
あなたの回答
tips
プレビュー