www.webdeveloper.com
Results 1 to 2 of 2

Thread: Return data from $.ajax call

  1. #1
    Join Date
    Feb 2013
    Posts
    21

    Return data from $.ajax call

    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.

    Here is the code that i wrote. Somehow this gives me errors.
    Code:
    bookNames : function(bookUrl){
      return $.get(bookUrl)
       .then(function(data){
        return data.map(function(b){
         return b.name; 
          }); 
       });
    }
    Here is the json that is passed as the bookUrl-

    Code:
    {
        "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?

  2. #2
    Join Date
    Feb 2013
    Posts
    21
    i managed to return a string by the following code -
    Code:
    bookNames : function(bookUrl){
      return $.get(bookUrl)
       .then(function(data){
        return data.books.map(function(b){
         return b.name; 
          }); 
       });
    }
    but how can i sort the names before returning?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles