ajax - Servlet json java -


i sending json using ajax jsp page, follows:

function daterca() {  var $tiputilizare = $("select[name=tiputilizare]").val(); var $datarca = $("input[name=datarca]").val();  var jsonobj = {"tiputilizare": $tiputilizare,     "datarca": $datarca};  alert($tiputilizare); alert($datarca);  $.ajax         (                 {                     url: 'asigurarerca',                     data: {daterca: json.stringify(jsonobj)},                     datatype: 'json',                     type: 'post',                     cache: false,                     success: function (raspuns) {                          alert("succes");                          if (raspuns.status.succes === "true") {                              $("#dateasigurarercatab").removeclass("active");                             $("#dateasigurarercatab").addclass("disabled");                             $("#dateasigurarecascotab").removeclass("disabled");                             $("#dateasigurarecascotab").addclass("active");                             $("#dateasigurarerca").removeclass("active");                             $("#dateasigurarerca").removeclass("in");                             $("#dateasigurarecasco").addclass("active");                             $("#dateasigurarecasco").addclass("in");                          }                         else {                             //handlde errors                         }                      },                     error: function () {                         alert('ne cerem scuze, dar aparut e eroare de sistem!');                      }                 }         );  } 

the form jsp file uses these functions is:

<div class="tab-pane fade" id="dateasigurarerca">                 <div  class="mainbox ">                     <div class="panel panel-info">                         <div class="panel-body" >                             <form name="dateasigurarerca" action="asigurarerca" method="post" class="form-horizontal" role="form">                                  <div class="form-group">                                     <label for="tiputilizare" class="col-md-6 control-label">tip utilizare autovehicul</label>                                     <div class="col-md-6">                                         <div class="col-md-6">                                             <select class="form-control"  name="tiputilizare" id="tiputilizare">                                                 <option value="">selecteaza tipul de utilizare</option>                                             </select>                                             <span style="color:red; display: none" class="eroaretiputilizare">  </span>                                         </div>                                     </div>                                 </div>                                  <div class="form-group" id="event_period">                                     <label for="datarca" class="col-md-6 control-label">data</label>                                     <div class="col-md-6">                                         <div class="col-md-6">                                              <div class="input-group">                                                 <input id="datepicker" type="text" class="date-picker form-control" name="datarca" value="" readonly/>                                                 </label>                                             </div>                                         </div>                                     </div>                                 </div>                                  <div class="form-group">                                     <!-- button -->                                                                             <div class="col-md-offset-3 col-md-9">                                         <input  id="btn-signup" type="submit" class="btn btn-info" name="nextasigurarecasco" value="continuare" onclick="daterca();">                                      </div>                                 </div>                              </form>                         </div>                     </div>                  </div>             </div>             <div class="tab-pane fade" id="dateasigurarecasco">                 date asigurare casco             </div> 

so, if servlet tells me information correct, need change tab , show next div.

here post method servlet handles data received (i using gson handle json):

@override protected void dopost(httpservletrequest request, httpservletresponse response)         throws servletexception, ioexception {      response.setcontenttype("application/json");     gson gson = new gson();      synchronized (getservletcontext()) {         servletcontext context = getservletcontext();         connectionpool cp = (connectionpool) context.getattribute("connection_pool");          system.out.println("ce preluat " + request.getparameter("daterca"));          string tiputilizare = request.getparameter("tiputilizare");         string datarca = request.getparameter("datarca");          enumeration<string> parameternames = request.getparameternames();          while (parameternames.hasmoreelements()) {              string paramname = parameternames.nextelement();             system.out.println("parametru name " + paramname);              string[] paramvalues = request.getparametervalues(paramname);             (int = 0; < paramvalues.length; i++) {                 string paramvalue = paramvalues[i];                 system.out.println("parametru value  " + paramvalue);             }          }          httpsession session = request.getsession();         connection con;          map<string, map<string, object>> mesaj = new linkedhashmap<>();         map<string, object> status = new linkedhashmap<>();         status.put("succes", "true");         mesaj.put("status", status);         response.getoutputstream().print(gson.tojson(mesaj));         response.getoutputstream().flush();      }  } 

the problem request.getparameter("daterca") returns null. jsp page following (those request parameters , values ):

info:   param name tiputilizare info:   param value  paza si protectie info:   param name datarca info:   param value  06/16/2015 info:   param name nextasigurarecasco info:   param value  continuare 

it gives me button's value, if don't send ajax ...

what servlet valid json: {"status":{"succes":"true"}}, instead of changing active tab , div, redirects me servlet link, showing me json object sent.

i used same method work other forms , worked great.. , after few hours of trying figure out how solve this... , finding no reason why doesn't work should.. ask you, if u can see problem , how can solve it.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -