1
votes

I tried to consuming api thorugh Axios in VueJS. But it giving error when i tried to fetch data. console log(res.data). Need your help. looks like i missed something

Uncaught (in promise) TypeError: Cannot read property 'protocol' of undefined

here's the code in API.Js

import axios from 'axios';
import API from '../API';



var urlLogin = API.url.host + '/login';

var login = {
    init: function(){
        this.vueConfig();
        if(localStorage.getItem('token') != null){
            window.location.replace("./input-mobile.html");
        }
    },
    vueConfig: function(){
        var app = new Vue({
            el: '#app',
            data: {
                isSubmit: false,
                email: "email",
                password: "password",
            },
            methods: {
                submitLogin: function(){
                    this.isSubmit = true;
                    axios.post()
                    axios({
                        method: 'post',
                        url: urlLogin,
                        data: {
                            email: this.email,
                            password: this.password
                        }
                    }).then(res =>{
                        console.log(res.data);
                        this.isSubmit = false;
                        localStorage.setItem("token", res.data.access_token);
                        localStorage.setItem("name", res.data.fullname);
                        window.location.replace("./input-mobile.html");
                    }, err =>{
                        console.log(err);
                        this.isSubmit = false;
                    });
                }
            }
        });
    }
}

module.exports = login;

API is ok. in network (inspect) of browser api giving correct response. But it failed to fetch data

1
just remove this line: axios.post() in your submitLogin methodSovalina
it works thank you very muchNelvan Balthazar

1 Answers

-1
votes

You're referencing the wrong object in your Axios callback. Try adding let self = this at the beginning of your submit method and then in your callback, change this.isSubmit by self.isSubmit.

submitLogin: function(){
  let self = this;
  this.isSubmit = true;
  axios.post()
  axios({
     method: 'post',
     url: urlLogin,
     data: {
         email: this.email,
         password: this.password
     }
  }).then(res =>{
     console.log(res.data);
     self.isSubmit = false;
     localStorage.setItem("token", res.data.access_token);
     localStorage.setItem("name", res.data.fullname);
     window.location.replace("./input-mobile.html");
  }, err =>{
     console.log(err);
     self.isSubmit = false;
  });
}