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它,因为它超出了范围。什么是这样做的reccomeneded方式?
你试过了吗?你写的时候''admin_data'肯定是可以访问的。 –
@MikeC是的,我已经尝试过。 'console_data'未定义在console.log()所在的位置。我还为第一个ajax调用添加了console.log(),并记录了正确的数据。见修订。 – ApathyBear
然后,您必须将其重新分配到某处,而不是向我们展示。 [你的代码工作得很好。](https://jsfiddle.net/w5w293vz/) –