Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send array from jQuery and display it in grails controller?

I have a array that contains an array of 10 number in jquery named "winnerList" I am sending it to WinnerController from jquery using ajax as :

$.ajax({
    url: "/demoapp/winner/winners",
    type: "POST",
    data: { "winnerList": winnerList },
    success: function(data) {
        alert("Successfully sent winnerList")
    },
});

Now I am receiving it inside controller using "winners" function as:

def winners(){
    print params
}

I am getting result as :

[winnerList[0][]:076, winnerList[1][]:118,winnerList[2][]:102, .....winnerList[9][]:18, action:winners, format:null, controller:winner]

But I wanted a result of only numbers not this type of structure which i could not access. Can anyone help?

like image 224
john smith Avatar asked Dec 03 '22 17:12

john smith


2 Answers

First convert your array to json when send it:

...
data: { "winnerList": JSON.stringify(winnerList) },
...

Then in the action parse this json:

def slurper = new JsonSlurper()
def result = slurper.parseText(params.winnerList)
like image 68
Samir Avatar answered Dec 22 '22 08:12

Samir


I got a solution for my own question.... In jquery:

............
data: { "winnerList": JSON.stringify(winnerList) },
.......

In controller:

...............
def listWinnerIds= JSON.parse(params.winnerLists)
..............

this will give the values in a list like [1,2,3]

like image 31
john smith Avatar answered Dec 22 '22 07:12

john smith