[FONT=Verdana]I need to get an array of srings with Java that I send from ajax, with jQuery, thus:
data: { namesArray : namesAfterPick }
with Java I grab thus:
namesArr = request.getParameterValues("namesArray");
but it’s not working… it’s not getting sent (or not getting received by the back-end…
complete code to send:
$.post( "infoArr.jsp",{
data: { namesArray : namesAfterPick }
} , function( data ) {
$( "#outputDivArr" ).append( data );
})
but for grabbing a single string, this
nameChosen = getParamater("namePicked");
works fine when sending thus:
$.post( "info.jsp",{
namePicked: names[0]
} , function( data ) {
$( "#outputDiv" ).append( data )
})
how do I grab with Java an array of strings sent with ajax???
thank you…[/FONT]
What datatype is namesArr in your Java?
What error messages are you receiving?
request.getParameterValues(“namesArray”) is a String. If you want to turn it into an array, you’re going to need to do something like:
String tempStr = request.getParameterValues("namesArray");
String[] namesArr = tempStr.split(",");
If you’re passing it via JSON Array, ie: [1, 2, 3, 4, 5] , then it would probably be better to use the JSONArray object, rather than dealing with a regex or something. http://www.json.org/javadoc/org/json/JSONArray.html
Also, is there a reason why getParamater(“namePicked”) has no HttpContext handler and request.getParameterValues(“namesArray”) does?
[FONT=Verdana]
mawburn:
What datatype is namesArr in your Java?
oh brother… I was almost done… then hit “reload” by mistake, lost everything… I thought this thing did autosave?
ok… as I mentioned in my OP, it’s an array of strings…
I decl/init the array thus:
String[] namesArr;
namesArr = request.getParameterValues("namesArray");
but now when do this:
String tempStr = request.getParameterValues("namesArray");
String[] namesArr = tempStr.split(",");
get error:
An error occurred at line: in the jsp file: /infoArr.jsp
Type mismatch: cannot convert from String to String
What error messages are you receiving?
none, it’s just not printing the array on return from the ajax…
request.getParameterValues(“namesArray”) is a String. If you want to turn it into an array, you’re going to need to do something like:
String tempStr = request.getParameterValues("namesArray");
String[] namesArr = tempStr.split(",");
If you’re passing it via JSON Array, ie: [1, 2, 3, 4, 5] , then it would probably be better to use the JSONArray object, rather than dealing with a regex or something. http://www.json.org/javadoc/org/json/JSONArray.html
Also, is there a reason why getParamater(“namePicked”) has no HttpContext handler and request.getParameterValues(“namesArray”) does?
yes… I use a special method to get param (single params…) long story… forgot to rewrite it here properly for purposes of this post…
thank you…
[/FONT]
[FONT=Verdana]
so do I have to use JSON?
it’s a simple string array… when user hits a button, I shuffle it, select first element, then remove it…
then when another user hits a button, I shuffle again, select first item again, then remove it… etc…
what I need to save to the server is the array after I have removed that first selected item…
why would I need to go thru JSON for this?
thank you…
[/FONT]
You don’t have to use JSON, I was just suggesting it might be easier if you’re already using it somewhere else in your code and already have the proper libraries. Since something like [1,2,3,4] is a Javascript array, where as an array of parameters is just 1,2,3,4.
In either case, it looks like the string is being split. The error:
An error occurred at line: [] in the jsp file: /infoArr.jsp
Type mismatch: cannot convert from String[] to String
Looks like it’s coming from somewhere else in your code, where you’re trying to use the String Array as a String. Does it really say “line: ” or does it say “line: 123” or something?
You need to access the array members.
for(int i=0;i<namesArr.length;++i) {
System.out.println(namesArr[i]);
}