JSON中意料之外的令牌

问题描述 投票:0回答:1

我使用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上。我不知道我错过了什么

javascript html firebase express
1个回答
0
投票
[根据我编写REST API的经验,通常会在API本身中遇到语法错误或异常时发生此错误。该代码看起来不错,但是HTML错误500指示它不在事物的客户端。

我没有使用firebase和express的经验,我主要使用Swagger和php编写REST API。


0
投票
这可能是由于JSON正文内容存在某些问题。诸如未转义的双引号。检查正在发送和接收的内容。

在此处了解更多信息-https://www.kevinleary.net/syntax-error-unexpected-token-json-position-0/

© www.soinside.com 2019 - 2024. All rights reserved.