Javascript AJAX get请求无法识别提交的数据?

Javascript AJAX get请求无法识别提交的数据?,javascript,firebase,firebase-realtime-database,firebase-authentication,Javascript,Firebase,Firebase Realtime Database,Firebase Authentication,我在客户端运行了以下代码,以访问数据库中用户的属性 firebaseAUTH.signInWithEmailAndPassword(email, password).then(function (user) { console.log('user has signed in with e-mail address: '+user.email+' and user ID: '+user.uid) firebaseAUTH.currentUser.getToken(true).then(functi

我在客户端运行了以下代码,以访问数据库中用户的属性

firebaseAUTH.signInWithEmailAndPassword(email, password).then(function (user) {
console.log('user has signed in with e-mail address: '+user.email+' and user ID: '+user.uid)
firebaseAUTH.currentUser.getToken(true).then(function(idToken) {
$.ajax(
    {
// Send token to your backend via HTTPS (JWT)
      url: '/auth',
      type: 'POST',
      data: {token: idToken},
      success: function (response){
      var userID = response.userID
  firebase.database().ref('/users/' + userID).once('value').then(function(snapshot) {
  var industry = snapshot.val().industry
  var company = snapshot.val().company
  var firstname = snapshot.val().firstname
  var email = snapshot.val().email
  var source = snapshot.val().source
  var phone = snapshot.val().phone
  var password = snapshot.val().password
  var lastname = snapshot.val().lastname
      $.get(
        {
          url: '/members-area/'+userID,
          data: {userID: userID,
                industry: industry,
                email: email},
          success: function(response){
          window.location = '/members-area/'+userID
          }
      })
我的服务器端代码:

app.get('/members-area/:userID', function(req,res,next) {
  res.render('members-area', { userID: req.params.userID, industry: req.params.industry, email: req.params.email})                                                                  
})

但是,当我尝试访问pug中的'industry'变量时,它显示为undefined。正如您所看到的,我在上面的getajax调用中发送了它,那么问题是什么呢?这也很奇怪,因为我在控制台上记录了函数快照之后的变量名,它们就在那里。此外,“userID”神秘地显示为内容的变量,但“industry”和“email”根本不存在。

我不太清楚您想做什么,但希望我能帮到一些人

首先,你不需要第二个电话就能拿到代币。当您使用Email和Password调用Signin时,firebase将返回用户。所以你可以马上打电话给getToken

firebaseAUTH.signInWithEmailAndPassword(email, password).then(function (user) {
console.log('user has signed in with e-mail address: '+user.email+' and user ID: '+user.uid)
console.log('we also got the token: ' + user.getToken());
...
您似乎也在发布未定义的路由,然后使用get查询不同的路由

此外,神秘的“userID”显示为一个变量,其中包含内容,但 “行业”和“电子邮件”根本不存在

在服务器端代码中,路由仅使用一个参数定义:userID。线路

app.get('/members-area/:userID', function(req,res,next) 
将userID定义为参数,而不是其他两个变量。因此,它们没有定义是有道理的

我想你想做的是:

firebaseAUTH.signInWithEmailAndPassword(email, password).then(function (user) {
    const userId = user.getToken();
    firebase.database().ref('/users/' + userID).once('value').then(function(snapshot) {
        $.post('/members-area/' + userId, snapshot.val(), function(data, status) {
           console.log(data);
           console.log(status);
    });
});
然后在服务器代码中:

app.post('/members-area/:userID', function(req,res,next) {
  const theSnapshot = req.body;
  res.send(theSnapshot)
});
我仍然不明白,为什么您希望使用客户机代码从数据库中检索信息,然后将其发布到服务器,然后再次获取。但也许我误解了什么:)


看到一个发送数据的get请求也很奇怪,我很确定这是违反规范的。您通常希望使用post发送数据,然后使用get to ehm获取数据:)

非常感谢!这正是我所需要的。我是否应该使用post或get作为私有/受保护的页面,即是否有任何约定?我认为这是有意义的:加载一个新页面(从系统中检索信息)。页面是否为私有/受保护并不重要。Post和Get总是以相同的方式使用。当您想向服务器发送数据时发布。然后,服务器对数据进行处理。当您要从服务器检索数据并对数据执行某些操作时获取。