项目场景:
POST http://localhost:3000/users/getadminlistmsg 404 (Not Found)
Uncaught (in promise) ReferenceError: Message is not defined
at eval.
提示:这里简述项目相关背景:
前后端分离项目,elem+vue+node.js+express
问题描述
提示:这里描述项目中遇到的问题:
在前端调用的是get请求,postman上面测试能够从接口获取数据,但是浏览器返回post失败,我尝试把后端改成post(用postman测试,依然可以得到数据),然后前端不出现POST404了,但是拿不到数据,还是显示后面Uncaught的错误
后端代码:
/* 获取管理员列表 */
static getAdminList(){
return new Promise((resolve,reject)=>{
let sql = "select * from myusers_db where type=3"
this.query(sql).then(result=>{
console.log(result)
resolve(result)
}).catch(err=>{
console.log("数据读取失败")
reject("数据读取失败了!")
})
})
}
/* 获取管理员列表 */
static async getAdminListMsg(req,resp){
let body = req.body
let adminData = await this.getAdminList()
if(adminData.length!=0){
console.log("读取管理员数据成功")
resp.send(adminData)
} else{
console.log(adminData)
console.log(adminData.length)
resp.status(500).send("读取管理员数据失败")
}
}
/* 获取管理员列表接口 */
router.get('/getadminlistmsg',function(req,res){
user.getAdminListMsg(req,res)
})
前端代码
封装的axios
/**
//封装axios方法,为不需要登录操作时使用
@param options 配置
/
let Axios=(options)=>{
axios({
url:options.url,
method:options.method||'POST',
data: options.data,
params: options.data
}).then(result=>{
console.log("成功进入axios")
if (options.success) options.success(result.data)
}).catch(err=>{
let msg = err.response ? err.response.data:'请求异常'
if (options.error){
options.error(msg)
Message.error({message: msg, offset: 150});
}else {
Message.error({message: msg, offset: 150});
}
})
}
Vue.prototype.$axios = Axios
前端页面在created获取数据:
created () {
this.$axios({
url:'/users/getadminlistmsg',
methods:'GET',
data:{},
success:(result)=>{
this.id = result.id
this.type = "管理员"
username = result.username
}
})
},
浏览器返回:
POST http://localhost:3000/users/getadminlistmsg 404 (Not Found)
Uncaught (in promise) ReferenceError: Message is not defined
at eval.
原因分析:
提示:这里填写问题的分析:
例如:未在前端main.js引入message组件,封装的axios有问题
解决方案:
提示:这里填写该问题的具体解决方案:
例如:将存在问题解决即可!