我的AJAX代码(将Firebase令牌发送到服务器)有什么问题?

我是初学者。 我试图通过AJAX从login用户的状态发送的令牌发送。 但是,当我运行下面的代码,我得到内部服务器错误,所以一些东西closures。 我错过了什么?

app.post('/auth', function(req,res,next){ var idToken = admin.auth().verifyIdToken(idToken) .then(function(decodedToken) { var uid = decodedToken.uid console.log(uid) res.send(uid) }).catch(function(error) { console.log(error.message) }) }) $("#sign-in").on('click', function(event) { event.preventDefault() var email = $("#email").val() var password = $("#password").val() firebaseAUTH.signInWithEmailAndPassword(email, password).then(function (user) { console.log('user has signed in with e-mail address: '+user.email+' and user ID: '+user.uid) //window.location = '/members-area' firebaseAUTH.currentUser.getToken(true).then(function(idToken) { $.ajax( { url: '/auth', type: 'POST', data: idToken, success: function (response){ console.log('sent successfully') console.log(uid)} } ) console.log(idToken) // Send token to your backend via HTTPS (JWT) }).catch(function(error) { // Handle error console.log(error.message) }) }).catch(function(error) { $('#errorbox').html('Error: '+error.message).css('color', 'red') }) }) 

你的代码有两个问题

对于服务器端,您应该从请求主体获取值

 app.post('/auth', function(req,res,next){ var idToken = req.body.idToken; admin.auth().verifyIdToken(idToken) .then(function(decodedToken) { var uid = decodedToken.uid console.log(uid) res.send(uid) }).catch(function(error) { console.log(error.message) }) }) 

和客户端,你应该正确地发送它

 $.ajax( { url: '/auth', type: 'POST', data: { idToken : idToken }, success: function (response){ console.log('sent successfully') console.log(uid)} })