问题遇到的现象和发生背景
在前端调用的是get请求,postman上面测试能够从接口获取数据,但是浏览器返回post失败。
问题相关代码,请勿粘贴截图
后端代码:
/* 获取管理员列表 */
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({
}).then(result=>{url:options.url, method:options.method||'POST', data: options.data, params: options.data
}).catch(err=>{console.log("成功进入axios") if (options.success) options.success(result.data)
})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.
我的解答思路和尝试过的方法
我尝试把后端改成post(用postman测试,依然可以得到数据),然后前端不出现POST404了,但是拿不到数据,还是显示后面Uncaught的错误