0

I am trying to append data that gets returned from a ajax call. I have the ajax call working correctly. However the data returned is one giant character array. I am using grails MVC, and the value I am returning is a ArrayList. I want to be able to run a for loop that will go through the items. But in order to do that I want to know if there is a way to convert 'data' into an array. The output is shown below.

function getAllActivitiItems() {
    console.log ("Starting function");
    $.ajax({
        url: '${g.createLink(controller: 'Activiti', action: 'getAllActivitiQueue')}',
        type: 'POST',
        success: function(data) {
            console.log("Made it! "+data);
        },
        error: function(data) {
            console.log(data);
        }
    });
}

AJAX return data value:

[
  ['userId':'FP301', 'taskId':'1', 'storeKey':'001E0', 'callKey':'12634'], 
  ['userId':'TS206', 'taskId':'2', 'storeKey':'00IAC', 'callKey':'12758'], 
  ['userId':'SN304', 'taskId':'3', 'storeKey':'00IAC', 'callKey':'09843']
]
JustAnotherUser32
  • 137
  • 2
  • 5
  • 17
  • this link could you help [link](http://stackoverflow.com/questions/2342371/jquery-loop-on-json-data-using-each) or you can use JSON.parse(data) – Sebastian Vega Aug 26 '14 at 20:28
  • Link didn't help, I am returning a List containing a map however the data in ajax success thinks its a character array. I am trying to print the data, but can't figure out why its returning it as a character array. – JustAnotherUser32 Aug 27 '14 at 15:20

1 Answers1

0

Are you looking to have an array of objects? If so, you may need to alter your server's getAllActivitiQueue method to return the data like this:

[
    {'userId':'FP301', 'taskId':'1', 'storeKey':'001E0', 'callKey':'12634'}, 
    {'userId':'TS206', 'taskId':'2', 'storeKey':'00IAC', 'callKey':'12758'}, 
    {'userId':'SN304', 'taskId':'3', 'storeKey':'00IAC', 'callKey':'09843'}
]

see this jsfiddle

Alternately, you could try using JSON.parse

Community
  • 1
  • 1
Jason Millikan
  • 597
  • 4
  • 14
  • I am sending a array list that contains a map. I define List lists = new ArrayList(), then have a each statement (groovy) that adds items to a map. Once the each statement is completed I then add it too lists, and return. – JustAnotherUser32 Aug 27 '14 at 01:02
  • I'm calling AJAX call in index.gsp, which calls my controller. The only way now I can print out my variables when they return is by characters. Ex. Data[0] would print [, and so forth. – JustAnotherUser32 Aug 27 '14 at 01:12