当我在函数中调用函数时,它返回undefined
。当函数本身被调用时,它会返回我期望它返回的数据。当从另一个函数调用时,Javascript函数返回undefined
的功能:
function companyNumbers(account_id) {
$.ajax({
type: "GET",
url: "http://example.com/api/api_client_relationships/" + account_id,
dataType: 'json',
success: function(data) {
for(var i=0;i<data.length;i++){
console.log(companyNames(data[i].company_id)); // returns undefined
}
}
});
}
function companyNames(id) {
$.ajax({
type: "GET",
url: "http://example.com/api/api_company_names/" + id,
dataType: 'text',
success: function(data) {
return data; // returns valid result when not called within another function
}
});
}
data[i].company_id
距离返回的JSON响应解析的值。它充当传递给companyNames函数的参数。
异步调用不同步! – epascarello