2016-06-23 5 views
0

私はajaxコールを実行してトークンを取得していますが、いったん完了すると、別のajaxコールを実行して特定のエンドポイントにヒットします。これは、すべての機能の内にラップされているようなaddAdminと呼ば:ネストされたAjaxコールはその関数のパラメータを見ることができません

addAdmin: function(admin_data){ 

    console.log(admin_data) //returns an object 

    // Get the access tolken 
    var getAccessToken = $.ajax({ 
     type: "POST", 
     url: 'http://somesite.com', 
     data: d, 
     contentType: 'application/json', 
     success: function(response){ 
      console.log("Token success.") 
     }, 
     error:function(jqXHR, textStatus, errorThrown) { 
      console.log("request for token failed: ",jqXHR , textStatus, errorThrown); 
     }, 
     dataType: 'json' 
    }); 

    // when the ajax call is done (the token is received) 
    getAccessToken.done(function(data) { 

     console.log(admin_data) // returns undefined 

     // hit the add endpoint url 
     var downloadurl = $.ajax({ 
      type: "POST", 
      url: "http://somesite.com/add", 
      contentType: 'application/json', 
      data: admin_data, 
      success: function(response){ 
       ... 
      }, 
      error:function(jqXHR, textStatus, errorThrown) { 
       console.log("request for download url failed: ", textStatus, errorThrown, jqXHR); 
      }, 
      dataType: 'json' 
     }) 


    }) 

    }, 

を問題は、私はadmin_dataが第二AJAX呼び出しに見えるようにする必要があること、であるが、私はので、それをretieveする方法を見つけ出すことはできませんその範囲外です。これを再考した方法は何ですか?

+2

は、あなたがそれを試してみましたか? 'admin_data'は書かれているので、確実にアクセスできます。 –

+0

@MikeCはい私はそれを試しました。 'admin_data'は、console.log()がどこにあるかは未定義です。また、最初のajax呼び出しにconsole.log()を追加し、適切なデータを記録します。リビジョンを参照してください。 – ApathyBear

+3

それからあなたはどこかに再割り当てして私たちを見せないでください。 [あなたのコードはうまくいきます。](https://jsfiddle.net/w5w293vz/) –

答えて

0

は、あなたのAjaxの成功関数からの応答を使用し、パラメータ/引数として独自の関数にそれを渡します

success: function(response) { 
    this.getAccessToken(response); 
} 

getAccessToken = function(response) { 
    console.log(response); 
    $.ajax({ 
     ... 
     data: response, 
     ... 
    }); 
} 
関連する問題