简体   繁体   English

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

[英]AJAX get request does not recognize submitted data?

I have the following code ran on the client-side to access the properties of a user within the database. 我在客户端运行了以下代码,以访问数据库中用户的属性。

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
          }
      })

My server-side code: 我的服务器端代码:

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})                                                                  
})

However, when I try to access the 'industry' variable in pug, it shows undefined. 但是,当我尝试访问pug中的'industry'变量时,它显示为undefined。 As you can see I send it above within the GET ajax call so what is the problem? 如您所见,我在GET ajax调用中将其发送到上面,这是什么问题呢? It's also weird because I logged to the console the variables names'right after the function snapshot and they were there. 这也很奇怪,因为我在函数快照之后立即将变量名登录到控制台,并且它们就在那。 Also, mysteriously 'userID' shows up as a var with content but 'industry' and 'email' do not at all. 而且,神秘地“ userID”显示为具有内容的var,但“ industry”和“ email”却根本不显示。

I don't quite know what you're trying to do but hopefully I can help some. 我不太了解您要做什么,但希望我能帮上忙。

First off you don't need a second call to get the token. 首先,您不需要第二次调用即可获取令牌。 When you call signInWithEmailAndPassword firebase returns the user. 当您调用signInWithEmailAndPassword firebase返回用户时。 So you can call getToken right away 这样就可以立即调用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());
...

You also seem to be posting to a route that isn't defined, and then you query a different route with get. 您似乎也正在发布到未定义的路由,然后使用get查询其他路由。

Also, mysteriously 'userID' shows up as a var with content but 'industry' and 'email' do not at all. 而且,神秘地“ userID”显示为具有内容的var,但“ industry”和“ email”却根本不显示。

In your server side code your route is only defined with one parameter: userID. 在服务器端代码中,仅使用以下参数定义路由:userID。 The line 线

app.get('/members-area/:userID', function(req,res,next) 

Defines userID as a parameter, not the other 2 variables. 将userID定义为参数,而不是其他2个变量。 So it makes sense that they're undefined. 因此,它们没有定义是有道理的。

What I think you're trying to do is: 我认为您正在尝试做的是:

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);
    });
});

And then in your server code: 然后在您的服务器代码中:

app.post('/members-area/:userID', function(req,res,next) {
  const theSnapshot = req.body;
  res.send(theSnapshot)
});

I still don't understand why you would want to retrive information using client code from a database and then post it a server only to get it again. 我仍然不明白为什么您要使用数据库中的客户端代码检索信息,然后将其发布到服务器中以便再次获取。 But maybe I am misunderstanding something :) 但是也许我误会了:)

Its also very strange to see a get request that sends data, I'm pretty sure its against the specs. 看到发送数据的get请求也很奇怪,我很确定它违反规范。 You usually want to use post to send data and then use get to ehm get the data :) 您通常希望使用post发送数据,然后使用get to ehm获取数据:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM