【发布时间】:2021-07-01 23:15:18
【问题描述】:
我想创建一个中间件来检查令牌是否有效,但是 axios 无法读取。任何人都可以帮助我如何找到解决方案。这是我的代码:
中间件/authentication.js
export default ({ app, redirect }) => {
let data = {
'userid': app.$cookies.get('user_id'),
'token': app.$cookies.get('auth_token'),
};
app.$axios.post(`/tokenVerify`,data)
.then(res => {
console.log(res)
})
}
pages/index.vue
export default {
middleware: "authentication",
nuxt.config.js
modules: [
'@nuxtjs/axios',
],
axios: {
proxy: true,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Accept' : 'application/json, text/plain, */*',
'Access-Control-Allow-Methods' : 'GET, PUT, POST, DELETE, OPTIONS',
'Access-Control-Allow-Credentials' : true,
}
},
proxy: {
'/tokenVerify':{target:process.env.BASE_URL+'index.php/public/tokenVerify', pathRewrite: {'^/tokenVerify': ''}, changeOrigin: true},
'/vimeoEmbed':{target:'https://vimeo.com/api/oembed.json', pathRewrite: {'^/vimeoEmbed': ''}, changeOrigin: true},
},
【问题讨论】:
-
您不应该在您的身份验证中间件中调用
redirect吗?您所做的只是记录结果。 -
目前,我没有使用重定向。我只想阅读 axios 函数,.. 和 console.log 结果。但在那种情况下,axios 没有读取它,.. 而且它们在 Network/XHR 记录中也没有检测到
标签: javascript vue.js axios nuxt.js nuxtjs