简体   繁体   中英

How to receive arrays sent by Ajax in a .jsp file

Hi I wrote the below js code to send two arrays to a jsp file

$.post("scriptlet.jsp",
{ sg: array1[], st: array2[] },
function(data){
   alert("Data Loaded: " + data);
}
);

How do I retrieve them form the jsp side? I tried doing this inside scriptlet.jsp . But didn't work

<%
    String[] questionsList = (String)request.getParameter("sg");
%>

if i understand very well your problem , so you are trying to send JavaScript array to a jsp page , so here are my suggestion :

<script >
var tab = ["hello","world"]; //here is an array for test
$.get("scriptlet.jsp",{ sg:tab  },function(data){
        alert("Data Loaded: " + data);
    }
);
</script>
</head>
<body>
<% 

String[] myArray = request.getParameterValues("sg[]");
out.println(myArray[0]); // this will print you  "hello"
%>

</body>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM