2

Just started with vuejs with Laravel , i am trying to make post request with axios http client library. Requests works fine but i can't able to display error messages in html file.

Here is the html file

<form @submit.prevent="addJob" >

  <input type="text" v-model="user.name" >
  <textarea v-model="user.message"></textarea>
  <button >Submit</button>
  //for showing errors
  <span v-if="error">{{error}} </span>
  </form>

Js

export default {

        data:function(){
           return {
              test:'hello',
              more:[
                      { message: 'Foo' },
                      { message: 'Bar' }
                   ],
              origin:'',
              user:{},
              error:''
           }
        },
        methods:{
          addJob:function(e){
             axios.post('/test',this.user)
               .then(function (response) {
                 //set the error message
                 this.$set('error',response.data.errors);
                 //this will returns nothing
                 console.log(error);
               })
               .catch(function (error) {

               });
          }

        }
    }

However the response.data.errors returns the error messages but i can't able to display in html page.

1 Answer 1

1

scope of this is not correct, instead use arrow functions, which don't bind their own scope:

  addJob:function(e){
     axios.post('/test',this.user)
       .then(response => {
         this.$set('error',response.data.errors);
       })
       .catch(function (error) {

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

2 Comments

woked when i change to this.error=response.data.errors
One more question how can i show success/error message template after ajax request

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.