[英](Axios) HTTP POST request failed after two minutes, Laravel 5 + VueJS
[英]axios returns 404 on first post request
function verNotasAluno(e) {
e.preventDefault()
setMateria(String(responseProfessor[1]))
axios.post(`http://localhost:3001/getNotasAluno/${materia}`, { lupaAluno }).then((response) => {
if (response.data === 'Erro') {
alert('Aluno não encontrado')
} else {
setNotasResponse(JSON.stringify(response.data))
}
})
}
return(
<div>
<form onSubmit={verNotasAluno}>
<span>Nome:</span> <input type={'text'} onChange={(e) => { setLupaAluno(e.target.value) }} required /> <br />
<br />
<button>Buscar Nota</button>
</form>
</div>
)
服务器端:
app.post('/getNotasAluno/:materia', (req, res) => {
const nome = req.body.lupaAluno
const materia = req.params.materia
let searchquery = ''
if (materia === 'Fisica') {
searchquery = 'SELECT nota_fisica, nota_fisica2, nota_fisica3 FROM pessoas where nome = ?;'
} else if (materia === 'Portugues') {
searchquery = 'SELECT portugues, portugues2, portugues3 FROM pessoas where nome = ?;'
} else if (materia === 'Matematica') {
searchquery = 'SELECT matematica, matematica2, matematica3 FROM pessoas where nome = ?;'
}
db.query(searchquery, nome, (err, result) => {
if (result.length === 0) {
res.send('Erro')
} else {
res.send(result)
}
})
})
正如我在标题中所说,我第一次提出提交表单的请求时,它给出了这个错误:点击查看图像错误但是如果我在第一个请求之后再次提出请求,它可以正常工作我是初学者,可以有人对我有所启发吗?
我发现出了什么问题,'materia' 参数对服务器来说是空的,这就是为什么第一个请求给出 404。不得不使用 UseEffect 来更新 useState 或其他东西。 感谢任何看到此代码的人。
从您的错误图片来看,发布请求需要材料作为 slug,但您没有在第一个请求中发送它。
在您的第一个 Api 通话中,您只是在通话
http://localhost:3001/getNotasAluno/
相反,它应该是
http://localhost:3001/getNotasAluno/1
1 是材料 ID
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.