How to pass javascript arrays to ajax send() method and retrieve them on jsp page ?

I have following html page:
Here I have passed url to ajax's send method and retrieved those values on jsp page.
How to pass multiple arrays instead of url to send() method, and how to retrieve those arrays on jsp ?
The code is from Expert rrz.

The html page :
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<script type="text/javascript">
function ajaxFunction(){
	var ajaxRequest; 
	var parameters="product=Mobile&location=India&year=2014 ";
	try{
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
				alert("Error!!");
				return false;
			}
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
                if (ajaxRequest.status === 200) {
                } else {
                        alert('Error: ' + ajaxRequest.status); 
                       }
			    //document.myForm.result.value = ajaxRequest.responseText;
				document.getElementById("result").value = ajaxRequest.responseText;
	    }
	}
	ajaxRequest.open("POST", "rrz_ajax.jsp", true);
	ajaxRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	ajaxRequest.send(parameters); 
}
</script>
</head>
<body>
Click on Send button.<br/>
<input type='button' onclick="ajaxFunction();" value="Send" /><br/>
Result from server: <input type='text' id='result' size="100"/>
</body>
</html>
                                      

Open in new window


The jsp page :
<%
String product = request.getParameter("product");
String location = request.getParameter("location");
String year = request.getParameter("year");
%>

Open in new window

Pradip ShenolkarAsked:
Who is Participating?
 
Pradip ShenolkarAuthor Commented:
I found solution :

html page
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<script type="text/javascript">

function ajaxFunction(){
	var ajaxRequest; 
	var parameters="product=Mobile&location=India&year=2014 ";
	var pr=["mobile","tv","laptop"];
	var prs=["2010","2013"];
	var products="products="+pr+"&time="+prs;
//	
	
	try{
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
				alert("Error!!");
				return false;
			}
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
                if (ajaxRequest.status === 200) {
                } else {
                        alert('Error: ' + ajaxRequest.status); 
                       }
			    //document.myForm.result.value = ajaxRequest.responseText;
				document.getElementById("result").value = ajaxRequest.responseText;
	    }
	}
	ajaxRequest.open("POST", "rrz_ajax.jsp", true);
	ajaxRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	ajaxRequest.send(products); 
}
</script>
</head>
<body>
Click on Send button.<br/>
<input type='button' onclick="ajaxFunction();" value="Send" /><br/>
Result from server: <input type='text' id='result' size="100"/>

</body>
</html>
                      

Open in new window


jsp page:
<%@ page import="mypkg.TestPost" %>
<%
String[] products=request.getParameterValues("products");
String[] time=request.getParameterValues("time");
%>

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.