I want to return an array from an ajax call. Here is the code that I have come up with so far. There are constraints that i need to follow. I cannot write additional functions outside of bookNames. The function signature of bookNames is fixed.
bookNames : function(bookUrl){
return $.ajax({
url: theUrl
}
I tried to separate out the book names by using the success callback but was not able to return the array since it is undefined. Here is the code written using success -
bookNames : function(bookUrl){
var allbooks = [];
$.ajax({
url: theUrl,
success : function(data){
for(var i=0; i<data.books.length; i++){
allBooks.push(data.books[i].name);
}
}
});
return allBooks;
}
Here is the json that is returned -
{
"books" : [
{ "name" : "b1" },
{ "name" : "b2" },
{ "name" : "b3" }
]
}
My problem is that i have to return an array of book names i.e ['b1', 'b2', 'b3']. I am kind of stuck now. How can i proceed?