ajax - Reading JSON values in servlet -


i posting jquery ajax post servlet , data in form of json string. not sure whether data getting posted or not. also, want verify login , password fetching json object.

heres code snippet:

<script src="http://code.jquery.com/jquery-latest.min.js"></script>     <script type="text/javascript" src="http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js"></script>      <script type="text/javascript">                 function doajax(){      var fullname=$("#fullname").val;     var mobileno=$("#mobileno").val;         $.ajax({          url: 'dvsds',          type: 'post',          datatype: 'json',          data:{ "mobileno":mobileno, "fullname":fullname},           error:function(){               alert("error occured!!!");           },           success:function(data){                alert(data.message);          }        });       } </script> 

my servlet side code:

public void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {      response.setcontenttype("application/json");     printwriter out = response.getwriter();     try {         string message=null;          jsonobject jobj = new jsonobject(request.getparameter("jsondata"));         iterator<string> = jobj.keys(); //gets keys          string name=null;         string mobileno=null;          while(it.hasnext())         {             string key = (string) it.next(); // key              if(key.equals("fullname"))                  name = (string)jobj.get(key);             else if(key.equals("mobileno"))                 mobileno=(string)jobj.get(key);         }           if(name=="abc" &&  mobileno=="123" )             message="success";         else             message="fail";          jsonobject jsonobject = new jsonobject();         jsonobject.put("message", message);         out.println(jsonobject); 

the datatype attribute of jquery.ajax function states following:

datatype (default: intelligent guess (xml, json, script, or html))

type: string

the type of data you're expecting server.

so, sending not json string. sending plain old request data.

you can data in servlet using:

string mobileno = request.getparameter("mobileno"); 

Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -