javascript - Vue.js 通过 Axios 使用 API 给出错误 : Uncaught (in promise) TypeError: Cannot read property 'protocol' of undefined

标签 javascript vue.js axios

我尝试在VueJS中通过Axios使用api。但当我尝试获取数据时它给出了错误。控制台日志(res.data)。需要你的帮助。看起来我错过了什么

未捕获( promise 中)类型错误:无法读取未定义的属性“协议(protocol)”

这是 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 没问题。在浏览器 api 的网络(检查)中给出正确的响应。但获取数据失败

最佳答案

您在 Axios 回调中引用了错误的对象。尝试在 submit 方法的开头添加 let self = this,然后在回调中将 this.isSubmit 更改为 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;
  });
}

关于javascript - Vue.js 通过 Axios 使用 API 给出错误 : Uncaught (in promise) TypeError: Cannot read property 'protocol' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55569696/

相关文章:

javascript - html/javascript中三个图像的随机位置

javascript - axios 拦截器响应未定义

laravel - Axios POST 到 Laravel API 会导致 419 错误,但在 postman 中工作正常

javascript - 使用javascript在图像上绘制

javascript - 如何向函数回调 Javascript 添加自定义属性

javascript - .arff格式的json怎么写

vue.js - 如何在 Vue 中修复 'Error in getter for watcher "isDark"'

vue.js - 如何使用 vuejs 制作可安装的 PWA?

javascript - 使用 VueJs 在 firebase 中上传和下载存储图像

reactjs - Ktor 后端的 CORS 问题