Node.js Vue2中标头中的Axios POST请求错误
我不熟悉javascript和Vue.js 我正在使用axios在Vuejs中进行POST API调用,我的后端在node.js中。我的授权有误。在《邮递员》中效果很好 我的POST API调用如下所示:Node.js Vue2中标头中的Axios POST请求错误,node.js,vue.js,vuejs2,axios,Node.js,Vue.js,Vuejs2,Axios,我不熟悉javascript和Vue.js 我正在使用axios在Vuejs中进行POST API调用,我的后端在node.js中。我的授权有误。在《邮递员》中效果很好 我的POST API调用如下所示: import Vue from 'vue' import router from '../router' import axios from 'axios' const API_URL = 'http://localhost:8000/api/' const
import Vue from 'vue'
import router from '../router'
import axios from 'axios'
const API_URL = 'http://localhost:8000/api/'
const LOGIN_URL = API_URL + 'login'
export default {
login(credentials, redirect) {
console.log(LOGIN_URL)
axios.post(LOGIN_URL,{
headers: {
'Authorization': 'Basic YWtzaGF5OmFrc2hheQ==',
'Content-Type': 'application/json',
'Accept': 'application/json'
},
data: {
'email': credentials.email,
'password': credentials.password
}
}).then(response => {
console.log(response.data)
router.push(redirect)
}).catch(e => {
console.log(e);
})
}
}
当我发出api请求时,我的javascript控制台显示
Failed to load resource: the server responded with a status of 500(Internal Server Error)
我的服务器控制台显示错误
TypeError: Cannot read property 'username' of undefined
这里的用户名是基本授权中的用户名。在您的数据帖子中,您没有发送用户名字段,只有电子邮件和密码,我认为您应该包含用户名。服务器不需要用户名。用户名字段用于标题授权您尚未包含所有相关代码。错误清楚地表明,它试图从一个空变量或未定义
username
的变量中读取属性username
。我们需要查看服务器代码才能获得更多帮助。服务器试图从哪个对象读取“用户名”字段?在您的数据帖子中,您没有发送用户名字段,只有电子邮件和密码,我认为您应该包含用户名。服务器不需要用户名。用户名字段用于标题授权您尚未包含所有相关代码。错误清楚地表明,它试图从一个空变量或未定义username
的变量中读取属性username
。我们需要查看服务器代码才能获得更多帮助。服务器试图从哪个对象读取“用户名”字段?