最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - passing json array ajax - Stack Overflow

programmeradmin4浏览0评论

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.

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.

Share Improve this question edited Jul 18, 2013 at 19:56 dfsq 193k26 gold badges242 silver badges259 bronze badges asked Jul 18, 2013 at 19:54 user1908568user1908568 891 gold badge1 silver badge13 bronze badges 4
  • info['hi','hello']...use this insted of what you are using.thats an old wat to do go about arrays. – HIRA THAKUR Commented Jul 18, 2013 at 19:56
  • You need to stringify your json data. – maketest Commented Jul 18, 2013 at 19:56
  • JSON.stringify(info); – Adriano Galesso Alves Commented Jul 18, 2013 at 19:57
  • :if you got your answer,tick the answer you feel is correct. – HIRA THAKUR Commented Jul 30, 2013 at 9:21
Add a ment  | 

3 Answers 3

Reset to default 10

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.

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);
    }
});

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/ to get a Java implementation of JSON parsing.

发布评论

评论列表(0)

  1. 暂无评论