【发布时间】:2020-05-07 18:50:14
【问题描述】:
我正在尝试为我的 API 创建一个登录系统,但我似乎无法在我的 Javascript 代码中得到响应。但是,在 POSTman 中,我可以:
https://i.stack.imgur.com/WneNm.png
这是我的 javascript 代码:
function loginUser(email, password) {
let person = {Email: email, Password: password};
fetch(UrlAuthenticationToken, {
method: "POST",
body: JSON.stringify(person),
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
})
.then((response) =>{
if (response.status === 200) {
console.log(`Logged in ${response.status}`);
return response.json(); // only for generating token
} else {
throw new Error(`error with status ${response.status}`);
}
})
.then((response) => {
let accessPass =
{
Token: reponse.Token,
User:
{
Email: reponse.User.Email,
Type: reponse.User.Type
}
}
sessionStorage.setItem(accessPass);
if(response.User.Type === 'Student'){
window.href(UrlStudent);
}
else if(response.User.Type === 'Lector'){
window.href(UrlLecturer)
}
else if(response.User.Type === 'Business'){
window.href(UrlCompany)
}
})
.catch((e) => {
Console.log(e);
});
};
问题是,我可以将我的 JSON 正文发送到后端,后端确实会返回响应,但前端似乎无法处理所述响应。我想知道出了什么问题;当我调试我的代码时,从第一个 .then 一直到最后,跳过其余部分。
【问题讨论】:
标签: javascript c# post fetch webapi