how to send javascript array to servlet using json

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ruby88
    New Member
    • Dec 2013
    • 4

    how to send javascript array to servlet using json

    Code:
    success : function(responseJson) {
    
    						var tbody = $("#Account");
    						alert("JSonResponse: " + responseJson);
    						$
    								.each(
    										responseJson,
    										function(index, account) { // Iterate over the JSON array.
    											var value = ATYpeMap[account.AccountTypeID];
    											$('<tr>')
    													.appendTo(tbody)
    													// Create HTML <tr> element, set its text content with currently iterated item and append it to the <table>.
    													.append(
    															$('<td>')
    																	.text(
    																			account.AccountNumber))
    													// Create HTML <td> element, set its text content with id of currently iterated account and append it to the <tr>.
    													.append(
    															$('<td>').text(
    																	value))
    													
    													// Create HTML <td> element, set its text content with name of currently iterated account and append it to the <tr>.
    													.append(
    															$('<td>')
    																	.text(
    																			account.AccountStatus))
    													.append($('<td style=\"display:none;\">').text(account.AccountTypeID))
    													.append(
    															$('<select id="Accountchange"+r+ onchange="dropDownOnChange(this)"><option value=""></option><option value="Valid">Valid</option><option value="Invalid">Invalid</option></select>'))
    											/* .append($('<button id="{account.AccountNumber}" value="ChangeAccountStatus" onclick="AccountChange(this)">ChangeAccountStatus</button>')) */;
    										});
    					}
    				});
    
    	};
     var item = [];
    	function dropDownOnChange(e) {
    		
    		 var selectedValue=e.options[e.selectedIndex].value;
    		alert("selectedValue:" + selectedValue);
    	 	 var currentRow= $(e).closest("tr");
    		  var AccountNo= $("td:eq(0)",$(currentRow)).text(); 
    	 	alert("accountno"+AccountNo);
    	 	var AccountType =$("td:eq(1)",$(currentRow)).text();
    	 	alert("acctyp"+AccountType);
    	 	var AcctypID= $("td:eq(3)",$(currentRow)).text();
    	 	alert("accID"+AcctypID);
    	 	
    		 var objddlvalue= {};
    		objddlvalue["AccountNo"] =AccountNo;
    		objddlvalue["AccountType"]=AccountType;
    		objddlvalue["Account Type_Val"]=AcctypID;
    		objddlvalue["AccountStatus"]=selectedValue;
    		 item.push(objddlvalue);
    		 console.log(item);
    		var jsonObj1 = JSON.stringify(item);
    		 //jsonObj1.parseJSON();
    		//JSONobj= JSON.parse(jsonObj1);
    	 	console.log(jsonObj1); 
    }
    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.
    Last edited by Dormilich; Dec 6 '13, 08:47 AM. Reason: Please use [CODE] [/CODE] tags when posting code.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    did you check what your HTTP request actually is?

    Comment

    • ruby88
      New Member
      • Dec 2013
      • 4

      #3
      Yes I set as application/json to it. I am getting json value as [{ "AccountNo" : "89348734", "AccountTyp e": "Credit", "Account Type_Val": "21", "AccountStatus" : "Invalid" }] Thank you.

      Comment

      • ruby88
        New Member
        • Dec 2013
        • 4

        #4
        Is it the right format? How can I loop through this value in servlet?

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          servlets are Java, not JavaScript.

          Comment

          • ruby88
            New Member
            • Dec 2013
            • 4

            #6
            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.

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              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()

              Comment

              Working...