Search code examples
javascriptlaravel-5.3vue.js

Vuejs won't let me console.log within request


In the methods section of my Vue.js script im trying to do a console.log but nothing is happening, in fact i can do no javascript at all within this section but the request to '/register' remains working.

postRegister: function(){
    //code here works fine
    console.log('working')

    this.$http.post('/register', this.user, function(response){
        //Any code here does not work even though the request itself has worked
        console.log('not working');                
        });
    }

My Controller

  public function Register(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'username' => 'required|unique:users|max:12',
            'email'    => 'required|unique:users|email',
            'password' => 'required|confirmed'
        ]);

        if ($validator->fails()) {
            return $validator->errors()->all();
        }
    }

As i originally said, everything works the validator returns the correct responses and everything and the post attempt is made successfully, there is no console data being logged however within the http post request!


Solution

  • Since you are using the validator you're not getting back a 200 status code and that's why the success function will not get triggered. You should catch the error and then console.log it:

    this.$http.post('/register', this.user)
       .then(function(response){
            console.log(response);                
        })
        .catch(function(error){
          console.log(error);
        });