【问题标题】:How to access laravel API with VUE JS?如何使用 VUE JS 访问 laravel API?
【发布时间】:2021-04-03 05:48:50
【问题描述】:

所以我想使用 mylogin api 但它不工作,即使电子邮件和密码不正确,它也会将路由推送到仪表板 这是我的代码

export default {
data(){
  return{
    form: {
    email: null,
    password: null
  },
    user: {},
    error: false
  }
},
methods: {
  login() {
    this.user.append("email", this.form.email);
        this.user.append("password", this.form.password);
        this.axios.post('http://127.0.0.1:8000/api/login', this.user).then(response => {
          localStorage.setItem("Name", response.data.first_name);
          this.$router.push('/userDashboard/Dashboard')
        });
  
  },
  register() {
    this.$router.push('/RegisterPage')
  }
},}

我的 laravel 路由接口

Route::post('/login', 'UserController@login');

登录功能

public function login(Request $request, User $user)
{
    
 $email = $request->input('email');
 $password = $request->input('password');

 $user = User::where('email', '=', $email)->first();
 if (!$user) {
    return response()->json(['success'=>false, 'message' => 'Login Fail, please check email']);
 }
 if (!Hash::check($password, $user->password)) {
    return response()->json(['success'=>false, 'message' => 'Login Fail, pls check password']);
 }
    return response()->json(['success'=>true,'message'=>'success', 'data' => $user]);
}

对不起我的英语

【问题讨论】:

  • this.user.append("email", this.form.email); 是否在 javascript 中抛出任何错误?
  • 没有,代码运行没有错误但API看起来好像没有被访问

标签: laravel api vue.js


【解决方案1】:

这是因为你的 laravel 应用总是返回 200 HTTP 响应,这导致前端的 .then( ... ) 总是被执行。

.then( ... ) 中检查您的 Laravel 设置的响应的 success 值,如下所示:

 this.user.append("email", this.form.email);
 this.user.append("password", this.form.password);
 this.axios.post('http://127.0.0.1:8000/api/login', this.user).then(response => {
   if (response.data.success === false) {
     // handle the error and stop the code with a return
     this.handleError();
     return;
   }
   localStorage.setItem("Name", response.data.first_name);
   this.$router.push('/userDashboard/Dashboard')
 });

或者,您也可以在 Laravel 中抛出 401400 响应以表示登录失败,这将在前端抛出异常,您可以使用 .then( ... ).catch( ... ) 捕获。

这是最干净的方式,因为不再需要发送'success' => true true,因为 HTTP 代码将成为所发生事情的真相来源。

public function login(Request $request, User $user)
{
    
 $email = $request->input('email');
 $password = $request->input('password');

 $user = User::where('email', '=', $email)->first();
 if (!$user || !Hash::check($password, $user->password)) {
    // never tell the person if it's email or password, always say it's one of both for security reasons
    return response(401)->json(['message' => 'Login Fail, please check email or password']);
 }

  return response()->json(['data' => $user]);
}

最后一点,我不明白this.user.append("email", this.form.email); 是如何工作的,因为this.user 似乎只是一个简单的对象,所以上面没有任何append 方法。

因此,除非我在这里遗漏了什么,否则最好的办法就是这样做:

const user = {
  email: this.form.email,
  password: this.form.password
}

// OR, make a copy
const user = { ...this.form }

// then send the user var to axios
this.axios.post('your url', user).then( ... )

【讨论】:

  • 嗨,感谢您对代码的快速响应,代码现在可以正常工作了!!非常感谢,你是我的救命稻草。XD
  • 很高兴我能提供帮助,请注意我的回答已解决,以便下一个遇到此问题的人解决。
猜你喜欢
  • 2018-06-22
  • 2021-05-31
  • 2017-04-11
  • 1970-01-01
  • 2019-03-15
  • 2021-07-06
  • 2020-08-30
  • 2022-11-11
  • 1970-01-01
相关资源
最近更新 更多