[英]HTTP request received with status 200 and without body
我正在使用 React、Node 和 MySQL 中的身份驗證制作小型 CRUD 應用程序。 所有這一切的初學者。 所以,我從后端獲取數據,在客戶端收到狀態為 200,但正文為空。 在 Chrome 開發人員工具的網絡選項卡中,我看到收到的響應數據。 前端、后端、DB都在一台機器上代碼:
return fetch(`http://localhost:4000/authenticate?email=${email}&password=${password}`)
.then(response => {
console.log(response)
response.json()
})
.then(response => {
console.log(response)
// login successful if there's a id in the response
if (response.id) {
// store user details in local storage to keep user logged in between page refreshes
localStorage.setItem('user', JSON.stringify(response));
dispatch(success(response));
dispatch(alertActions.clear());
history.push('/');
} else {
dispatch(failure(response.message));
dispatch(alertActions.error(response.message));
//dispatch(logout());
}
服務器:
app.get('/authenticate', (req, res) => {
let answer = { message: ''}
let sql = `SELECT * FROM users WHERE email = '${req.query.email}'`;
console.log(sql)
let query = db.query(sql, (err, result) => {
console.log(result)
if(err) {
throw err
} else {
if (result.length > 0) {
if (result[0].password === req.query.password) {
res.send(result)
} else {
answer.message = 'Email and Password does not match!'
console.log(answer)
console.log(JSON.stringify(answer))
res.send(JSON.stringify(answer))
}
} else {
answer.message = 'Email does not exists!'
res.send(JSON.stringify(answer))
}
}
})
});
你需要返回response.json()
使你的下一個then
可以收到它。 改變這個:
.then(response => {
console.log(response)
response.json()
})
到:
.then(response => {
return response.json()
})
或者甚至更短:
.then(response => response.json())
更新:在看到您的服務器代碼后,您可以嘗試另一件事。 您需要確保使用 JSON 進行響應。 在這里,嘗試更改此行:
res.send(result)
到:
return res.json(result);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.