By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,731 Members | 1,129 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,731 IT Pros & Developers. It's quick & easy.

how to send javascript array to servlet using json

P: 4
Expand|Select|Wrap|Line Numbers
  1. success : function(responseJson) {
  2.  
  3.                         var tbody = $("#Account");
  4.                         alert("JSonResponse: " + responseJson);
  5.                         $
  6.                                 .each(
  7.                                         responseJson,
  8.                                         function(index, account) { // Iterate over the JSON array.
  9.                                             var value = ATYpeMap[account.AccountTypeID];
  10.                                             $('<tr>')
  11.                                                     .appendTo(tbody)
  12.                                                     // Create HTML <tr> element, set its text content with currently iterated item and append it to the <table>.
  13.                                                     .append(
  14.                                                             $('<td>')
  15.                                                                     .text(
  16.                                                                             account.AccountNumber))
  17.                                                     // Create HTML <td> element, set its text content with id of currently iterated account and append it to the <tr>.
  18.                                                     .append(
  19.                                                             $('<td>').text(
  20.                                                                     value))
  21.  
  22.                                                     // Create HTML <td> element, set its text content with name of currently iterated account and append it to the <tr>.
  23.                                                     .append(
  24.                                                             $('<td>')
  25.                                                                     .text(
  26.                                                                             account.AccountStatus))
  27.                                                     .append($('<td style=\"display:none;\">').text(account.AccountTypeID))
  28.                                                     .append(
  29.                                                             $('<select id="Accountchange"+r+ onchange="dropDownOnChange(this)"><option value=""></option><option value="Valid">Valid</option><option value="Invalid">Invalid</option></select>'))
  30.                                             /* .append($('<button id="{account.AccountNumber}" value="ChangeAccountStatus" onclick="AccountChange(this)">ChangeAccountStatus</button>')) */;
  31.                                         });
  32.                     }
  33.                 });
  34.  
  35.     };
  36.  var item = [];
  37.     function dropDownOnChange(e) {
  38.  
  39.          var selectedValue=e.options[e.selectedIndex].value;
  40.         alert("selectedValue:" + selectedValue);
  41.           var currentRow= $(e).closest("tr");
  42.           var AccountNo= $("td:eq(0)",$(currentRow)).text(); 
  43.          alert("accountno"+AccountNo);
  44.          var AccountType =$("td:eq(1)",$(currentRow)).text();
  45.          alert("acctyp"+AccountType);
  46.          var AcctypID= $("td:eq(3)",$(currentRow)).text();
  47.          alert("accID"+AcctypID);
  48.  
  49.          var objddlvalue= {};
  50.         objddlvalue["AccountNo"] =AccountNo;
  51.         objddlvalue["AccountType"]=AccountType;
  52.         objddlvalue["Account Type_Val"]=AcctypID;
  53.         objddlvalue["AccountStatus"]=selectedValue;
  54.          item.push(objddlvalue);
  55.          console.log(item);
  56.         var jsonObj1 = JSON.stringify(item);
  57.          //jsonObj1.parseJSON();
  58.         //JSONobj= JSON.parse(jsonObj1);
  59.          console.log(jsonObj1); 
  60. }
When I click save button I want to send all dropdown chosen value and other table data to servlet. I am not been able to send to servlet .Please help me.
Dec 6 '13 #1
Share this Question
Share on Google+
6 Replies


Dormilich
Expert Mod 5K+
P: 8,639
did you check what your HTTP request actually is?
Dec 6 '13 #2

P: 4
Yes I set as application/json to it. I am getting json value as [{ "AccountNo": "89348734", "AccountType": "Credit", "Account Type_Val": "21", "AccountStatus": "Invalid" }] Thank you.
Dec 6 '13 #3

P: 4
Is it the right format? How can I loop through this value in servlet?
Dec 6 '13 #4

Dormilich
Expert Mod 5K+
P: 8,639
servlets are Java, not JavaScript.
Dec 6 '13 #5

P: 4
Yes I understand Dormilich.But what I am having problem is my json format form javascript. I am not been able to get correct jsonstringarray in javascript.As per above my code,I am taking values from every dropdown event change.and wanting to save those data to servlet. since my servlet could not loop through becuase of json format. Thank you.
Dec 6 '13 #6

Dormilich
Expert Mod 5K+
P: 8,639
the JSON rersponse given hardly matches with the AJAX coding, there is accountNo vs. accountNumber, accountTypeID does not exist, etc.

additionally, if the jQuery AJAX call is not set to json, you may need to parse the response first.

PS. console.log() is way more descriptive than alert()
Dec 6 '13 #7

Post your reply

Sign in to post your reply or Sign up for a free account.