In My.htm, I post a array var mytemp to server side, I hope to retrieve the array var in server side.
I only get the a string D 1,D,2,Tom'Dog if I use the following code, how can I retrieve the array in server side ? Thanks!
BTW, I hope to do a full post, not ajax, so I think that $.ajax() doesn't fit.
My.htm
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="js/jquery1.10.2.min.js?isassets=1" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
document.querySelector('#myformID').addEventListener('submit', event => {
if (!confirm('Do you want to delete selected images?')) {
event.preventDefault();
}
});
$("#myformID").submit( function(eventObj) {
var mytemp=new Array();
mytemp.push("D 1");
mytemp.push("D,2");
mytemp.push("Tome'Dog");
$('<input />').attr('type', 'hidden')
.attr('name', "myCw")
.attr('value',mytemp)
.appendTo('#myformID');
return true;
});
});
</script>
</head>
<body>
<div id="container">
<form action='' method='post' enctype='multipart/form-data' id="myformID">
<input type='file' name='myfilename' /> Please select a file
<input type='submit'name='submit' value='Delete Selecetd Items' />
</form>
</div>
</body>
</html>
Server Side
public class HttpServer extends NanoHTTPD {
private Context mContext;
public HttpServer(Context myContext) throws IOException {
super(PublicParFun.GetWebPort(myContext));
this.mContext=myContext;
start(NanoHTTPD.SOCKET_READ_TIMEOUT);
}
private Response POST(IHTTPSession session) {
Utility.LogError("Handle Post");
try {
Map<String, String> files = new HashMap<String, String>();
session.parseBody(files);
Set<String> keys1 =session.getParms().keySet() ;
for(String key: keys1){
String name = key;
String loaction =session.getParms().get(key);
}
} catch (Exception e) {
Utility.LogError("This is an error "+e.getMessage() );
System.out.println("i am error file upload post ");
e.printStackTrace();
}
return newFixedLengthResponse(Response.Status.OK, NanoHTTPD.MIME_PLAINTEXT,"I'm Hello!");
}
@Override
public Response serve(IHTTPSession session) {
String uri = session.getUri();
Method method = session.getMethod();
MURLPar mURLPar=new MURLPar(mContext);
FileFolderHelper.SetMURLParValue(mURLPar,session);
if (mURLPar.isassets.value!=null && mURLPar.isassets.value.equals("1")){
return GetResponseInputByAssetsFile(uri);
}
if (Method.POST.equals(method)) {
return POST(session);
}
String s=FileFolderHelper.GetStringTemplate(mContext,mContext.getString(R.string.WebImageBase));
return newFixedLengthResponse(s);
}
}
To Erfan Mowlaei: Thank you! Is the following code OK?
Client Side:
var mytemp=new Array();
mytemp.push("D 1");
mytemp.push("D, 2");
mytemp.push("D'3");
$("#myformID").submit( function(eventObj) {
$('<input />').attr('type', 'hidden')
.attr('name', "myCw")
.attr('value', JSON.stringify(mytemp))
.appendTo('#myformID');
return true;
});
Server Side
public ArrayList<String> jsonStringToArray(String jsonString) throws JSONException {
ArrayList<String> stringArray = new ArrayList<String>();
JSONArray jsonArray = new JSONArray(jsonString);
for (int i = 0; i < jsonArray.length(); i++) {
stringArray.add(jsonArray.getString(i));
}
return stringArray;
}