Sachin Verma
Sachin Verma

Reputation: 3802

get data from Ajax call other than string

I want to receive some data from a grails action via jquery's ajax call.
Till now i have used ajax call just to receive some string but now i want to receive some lists(many lists) .
here is my action

def getSpecificData() {
        **some code here**
        render (list:[10,20,30,40],list2:[20,30,40]);
    }

My jquery call:

jQuery.ajax({
                    type: 'POST',
                    url: graphUrl,
                    data: "xaxis="+$(this).val(),
                    async: true,
                    success: function(response,textStatus){
                        console.log(response);

                     },
                    error:function(XMLHttpRequest,textStatus,errorThrown){}
                });

The console log is ['list':[10, 20, 30, 40], 'list2':[20, 30, 40]] It is basically a string only but i want two list as response, is it possible?
I want those list1 and list2 as response data. Is there any way to do so?
I think i can make a json from lists(2 elements for two lists) and then convert json strings to array.
But that seem like mess so anyone have better idea?

Upvotes: 0

Views: 1158

Answers (1)

Elias Dorneles
Elias Dorneles

Reputation: 23806

Make the server render a map with the lists as JSON:

render ([list:[10,20,30,40],list2:[20,30,40]] as JSON)

You may need to import the JSON converter:

import grails.converters.JSON

And then make your AJAX look like this:

jQuery.ajax({
                type: 'POST',
                url: graphUrl,
                data: "xaxis="+$(this).val(),
                async: true,
                dataType: "",
                success: function(dataReceived,textStatus){
                    console.log('list:', dataReceived.list);
                    console.log('list2:', dataReceived.list2);
                 },
                error:function(XMLHttpRequest,textStatus,errorThrown){}
            });

You may want to consider using the shorthand jQuery.post(url, data, successCallback).

Upvotes: 1

Related Questions