1

Am trying to use laravel and vue js with axios

Previously this was my login form

<form name="login-form" method="POST" action="{{ route('login') }}">
 //username and password fields

</form>

Which works perfectly one can login

Now i would like to use vuejs in a similar way without making my app a single page app

so i have resulted to making a login component

 <template>
  <form name="login-form">
    <input type="email" v-model="email" class="form-control" autofocus>
    <input id="password" type="password" v-model="password"  required>

     <button type="submit"
             class="btn btn-dark btn-sm"
             @click.prevent="login" 
            :disabled="disableform">
            {{submitted?"Logging you in .....":"Login"}}
     </button>

  </form>
 </template>

Now script part

  <script>

    export default {
      data: () => ({
         email :'', password:'', remeberme:true, submitted:false
      }),
    methods: {
    login() {
     //do other stuff like disabling buttons...etc
     axios.post("/login", {email:this.email, password:this.password})
        .then(
            (res)=>{

              ///showing sweet alert then 
              // settimout for 3 sec the 
            window.location.href="/dashboard";
            },
            (err)=>{
             //show sweet alert of failed login
            }
        )
},

}

Now whenever i make the login post request an getting an error

Request failed with status code 422

I would like to proceed with the laravel vuejs workflow(with authentication sessions) but not an api jwt token request format.

What could be wrong or is this possible?

4
  • Did you add your csrf token to the request? Commented Sep 25, 2017 at 15:55
  • Yes in my bootstrap.js its already set even checking on the developer tools i can see it as X-CSRF-TOKEN:Gok4MlG3zgp95ic2tkDntQy9nOHTUsJoWE4PnXsh Commented Sep 25, 2017 at 15:59
  • let token = document.head.querySelector('meta[name="csrf-token"]'); window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content; Commented Sep 25, 2017 at 16:00
  • so csrf tokn is already there Commented Sep 25, 2017 at 16:00

2 Answers 2

1

It's a validation error. check you laravel controller regarding post/put function.

Request failed with status code 422

When laravel fail the validation then it appears.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to use a csrf_field in your form, like this:

<input type="hidden" name="_token" :value="csrfToken">

And define it in your script:

data() {
   return {
      csrfToken: ''
   } 
},
created() {
       this.csrfToken = document.querySelector('meta[name="csrf-token"]').content;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.