如何将变量移出范围?

时间:2017-05-07 14:57:28

标签: javascript

如何将变量userID移出其范围(一旦从ajax POST调用返回),因为我需要它的'value(在GET请求中)?我应该使用回调吗?

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) {
// Send token to your backend via HTTPS (JWT)
$.ajax(
    {
      url: '/auth',
      type: 'POST',
      data: {token: idToken},
      success: function (response){
      var userID = response.userID
      //set database ref, pull out info based on ID
      }})
$.get(
    {
      url: '/members-area/',
      data: {userID: userID}
    })

1 个答案:

答案 0 :(得分:3)

移动进入ajax成功?

$.ajax({
    url: '/auth',
    type: 'POST',
    data: {
        token: idToken
    },
    success: function (response) {
        var userID = response.userID

        $.get({
            url: '/members-area/',
            data: {
                userID: userID
            }
        })
    }
})