jquery - retrieve ajax posted values in servlet? -
var schedule = []; var data = { 'user_id' : '12', 'day_of_week' : 'monday', 'when' : 'start', 'modified' : 'true' } schedule.push(data); var data = { 'user_id' : '13', 'day_of_week' : 'tuesday', 'when' : 'end', 'modified' : 'false' } schedule.push(data); // schedule have 2 objects in
i posting array servlet using jquery ajax post request below .
data : {'mydata':schedule}, url :"./mycontroller",
now how can array of objects , literate values? each array object has string, long , boolean type values. how can values iterating?
one strategy use json pass kind of data , forth server , client.
on client side, convert array of schedule json string using stringify function. send via ajax:
e.g.
var jsondata = json.stringify(schedule);
your ajax object following:
$.ajax({ url: "./mycontroller", data: {'schedules':jsondata} });
now on server side use json parser google's gson library.
in servlet following:
string jsondata = request.getparamater("schedules");
parse using gson
gson gson = new gson(); list<schedule> schedules = gson.parsejson(jsondata, list<schedule>.toclass());
you can create schedule class map javascript object java object
i can provide better code tested if interested ... not have access ide @ moment ... hope general idea
Comments
Post a Comment