【问题标题】:Uncaught (in promise) csrf Vue js laravel未捕获(承诺)csrf Vue js laravel
【发布时间】:2023-02-03 18:15:42
【问题描述】:

我在 vuejs 中创建登录表单。我通过邮递员 api 测试运行良好。当我检查 vue js validtaion 时,它不起作用。登录错误,未捕获(承诺)csrf Vue js laravel。 到目前为止我尝试了什么我附在下面。我认为 json 验证问题你能检查一下吗。我在下面附上了完整的源代码。

Login.vue

    <template>

    <div class="row">
    
    <div class="col-sm-4" >
     <h2 align="center"> Login</h2>
   
     <form @submit.prevent="LoginData">
   
    <input type="hidden" name="_token" :value="csrf">
     <div class="form-group" align="left">
       <label>Email</label>
       <input type="email" v-model="student.email" class="form-control"  placeholder="Mobile">
     </div>


    <div class="form-group" align="left">
    <label>Password</label>
    <input type="password" v-model="student.password" class="form-control"  placeholder="Mobile">
  </div>

     <button type="submit" class="btn btn-primary">Login</button>
     </form>
   </div>
   </div>

</template>
   
   <script>
       import Vue from 'vue';
       import axios from 'axios';
   
     Vue.use(axios)
     export default {
       name: 'Registation',
       data () {
         return {
            csrf: document.querySelector('meta[name="csrf-token"]').getAttribute('content'),
           result: {},
           student:{
                      email: '',
                      password: ''
           }
         }
       },
       created() { 
       },
       mounted() {
             console.log("mounted() called.......");
         },
       methods: {
              LoginData()
              {
               axios.post("http://127.0.0.1:8000/api/login", this.student)
               .then(
                 ({data})=>{
                  console.log(data);
                  try {
                  if (data === true) {
                        alert("Login Successfully"); 
                        this.$router.push({ name: 'HelloWorld' })
                        } else {
                        alert("Login failed")
                        }

                        } catch (err) {
                        alert("Error, please try again");
                        }    
                 }
               )
              }
         }
     }
     </script>
     

登录控制器

public function check(Request $request)
{

 $credentials = $request->validate([
 'email' => ['required', 'email'],
 'password' => ['required'],
    ]);
    
    if (Auth::attempt($credentials)) 
    {
     
     return response()->json(['data' => true ]);

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

【问题讨论】:

  • 你现在可以检查我在 vue csrf 中附加的完整代码没有添加

标签: laravel vue.js


【解决方案1】:

根据 Laravel 9 文档,您必须发送 csrf 令牌。这是谈论它的链接:

https://laravel.com/docs/9.x/sanctum#csrf-protection

【讨论】:

  • 如何在此处添加令牌 axios.post("127.0.0.1:8000/api/login", this.student)
  • 我试过添加 csrf 令牌。我得到的问题是 Uncaught (in promise) TypeError: Cannot 。我更新了上面的vue代码
  • 你是什​​么意思?我不明白这个错误
  • 返回 { csrf: document.querySelector('meta[name="csrf-token"]').getAttribute('content'),
  • 我已经像这样添加了 csrf-token 但是当尝试登录时我收到错误 Uncaught (in promise) csrf
猜你喜欢
  • 2017-11-24
  • 2021-11-15
  • 2019-11-03
  • 2019-04-18
  • 2017-08-31
  • 2019-01-16
  • 1970-01-01
  • 2023-02-25
  • 2023-02-05
相关资源
最近更新 更多