When I call a function within a function it returns undefined
. When the function is called by itself it returns the data I expect it to return.
The functions:
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
is just a value parsed from the returned json response. It acts as the argument passed to the companyNames function.