【发布时间】:2021-11-06 21:09:57
【问题描述】:
我向我的 API 发送 post 请求,其中在正文中包含用户名和密码。当我使用 Postman 执行此操作时,我可以得到一个正文包含令牌的响应。
但是当我在前面发送相同的请求时,我得到了一个空正文的响应。
我的宝贝在这里:
async function login(){
await fetch('/auth/login',
{
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(
{
'userName' : userName,
'password' : password,
}
)
}
).then(
res => {
console.log(res)
localStorage.setItem('Authorization',JSON.stringify(res))
}
).catch(
err => {
console.log("Erorororor: " + JSON.stringify(err))
}
)
}
有什么问题?
【问题讨论】:
-
您是否尝试过记录 res.body 以查看是否有任何内容?也可能点击 (...) 会展开它
-
是的,只是 {} 太空了。
-
您是否尝试过 res.toJSON() 或其他方式来解析响应?
-
我试过 res.JSON(),结果一样。正文是空的。
-
您是否按照文档所说的那样放置了 .then ?所以它会是 (res => {res.JSON()}).then(json => console.log(json))
标签: javascript request fetch