user1908568
user1908568

Reputation: 89

passing json array ajax

Trying to pass a array using ajax call.

info = [];
info[0] = 'hi';
info[1] = 'hello';

$.ajax({
    type: "POST",
    data: {info: info, "action": "getPatientRecords"},
    url: "/mobiledoc/jsp/ccmr/webPortal/carePlanning/servicePatientmilestoneModal.jsp",
    success: function(msg) {
        $('.answer').html(msg);
    }
});

However when i try to catch it on the server side using : request.getParameter("info"); //Displays null**

Also, if i wish to send an array of arrays ? is it possible?

I tried using serialize however my IE throws error that serialize : object doesnt support this property i Did include jquery lib.

Upvotes: 2

Views: 15432

Answers (3)

Logan Murphy
Logan Murphy

Reputation: 6230

You can only provide strings in a request url.

You could encode the array like so:

info = JSON.stringify(info); 
// results in "['hi', 'hello']"

Then send it to the server, also JSON parse on the server.

You will need to go to http://www.json.org/ to get a Java implementation of JSON parsing.

Upvotes: 1

HIRA THAKUR
HIRA THAKUR

Reputation: 17757

Yes,It is Possible to send arrays.

var info_to_send = ['hi','hello'];

$.ajax({
    type: "POST",
    data: {info: info_to_send, "action": "getPatientRecords"},
    url: "/mobiledoc/jsp/ccmr/webPortal/carePlanning/servicePatientmilestoneModal.jsp",
    success: function(msg) {
        $('.answer').html(msg);
    }
});

Upvotes: 2

Explosion Pills
Explosion Pills

Reputation: 191729

You can use JSON.stringify(info) to create a JSON representation of the object/array (including an array of arrays). On the server side you should be able to get the string via getParameter and then unserialize it from JSON to create constructs JSP can use.

Upvotes: 10

Related Questions