我使用Firebase身份验证制作了一个身份验证系统。我像这样在express中创建了一个端点:
app.post('/users/login', (req, res) => {
console.log('logare...');
firebase.auth().signInWithEmailAndPassword(req.body.email, req.body.parola)
.then(user => {
res.json({ succes: true, msg: "te-ai autentificat cu succes" });
})
.catch(err => {
console.log('date invalide');
res.json({ succes: false, msg: 'date invalide', errCode: err.code });
})
})
然后我在字体结尾的js文件中创建了一个函数,用于向服务器发送请求:
const autentificare = async (email, parola) => {
return await fetch('https://cm-api-1.herokuapp.com/users/login', {
method: 'POST',
headers: {
'content-type': "application/json"
},
body: JSON.stringify({
email: email,
pasword: parola
})
})
.then(res => {
console.log(res.status)
return res.json();
});
};
在我的app.js文件中,我创建了针对真实电子邮件和密码的Submit事件:
form.addEventListener('submit', async e => {
e.preventDefault();
const email = emailDom.value;
const parola = parolaDom.value;
await autentificare(email, parola)
.then(data => {
if (data.succes) {
console.log('ok datele sunt valide')
} else {
console.log('date invalide')
}
})
});
[当我在控制台中提交表单时,它向我显示此错误:POST https://cm-api-1.herokuapp.com/users/login 500 (Internal Server Error)
和Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0
我的api已部署在heroku上。我不知道我错过了什么
我没有使用firebase和express的经验,我主要使用Swagger和php编写REST API。
在此处了解更多信息-https://www.kevinleary.net/syntax-error-unexpected-token-json-position-0/