如何解决从客户端处理数据时,React js中的代理错误
我在react js项目中使用代理。我在客户端获取数据时遇到问题。我在服务器端检查功能,并返回结果。但是,当我在UI中调用时,它使用客户端端口3000获取数据,这是错误的。并告诉我这个错误http://localhost:3000/api/teacher/allPublicStudentlist/ 404 (Not Found)
在这里,端口应该是3001。实际上,我获取了其他功能,它可以正常工作,但是专门针对此功能,它会返回404错误(未找到)。您能否说出错误可能在哪里?
PS:我已经将"proxy": "http://localhost:3001"
添加到了package.json
这是我的代码。
服务器端
//does not work
app.get('/api/teacher/allPublicStudentlist/:cid',(req,res) => {
dao.listAllPublicStudents(req.params.cid)
.then((students) => {res.json(students);})
.catch(() => {res.status(500).end();});
});
//work
app.get('/api/teacher/getCourseID/:name',res) => {
dao.getCourseId(req.params.name)
.then((students) => {res.json(students);})
.catch(() => {res.status(500).end();});
});
客户端
async function getCourseID(name) {
const response = await fetch('/api/teacher/getCourseID/'+name);
const result = await response.json();
if(response.ok){
return result;
} else{
throw {error: result}
}
}
async function getStudent(CourseID) {
const response = await fetch('/api/teacher/allPublicStudentlist/'+CourseID);
const result = await response.json();
if(response.ok){
return result;
} else{
throw {error: result}
}
}
const API = {getStudent,isAuthenticated,userLogin,userLogout,getTeacher,getCourseID} ;
export default API;
//does not work
getAllStudents=(courseID)=> {
API.getStudent(courseID).then((res) => {
this.setState({checkedStudents: res});
console.log(this.state.checkedStudents);
})
}
//work
getCourseID=(name)=>{
API.getCourseID(name).then((res) => {
this.setState({courseID: res[0].courseId})
console.log(this.state.courseID);
})
.catch((err) => {
this.setState({AuthErr : err.msg});
});
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。