2

I am posting a jQuery AJAX POST to a servlet and the data is in the form of JSON String. I am not sure whether data is getting posted or not. Also, I want to verify login and password by fetching it from json object.

Heres the 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> it = jObj.keys(); //gets all the keys

        String Name=null;
        String mobileno=null;

        while(it.hasNext())
        {
            String key = (String) it.next(); // get 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);
shanky
  • 751
  • 1
  • 16
  • 46

2 Answers2

0

You do not need to stringify the data ... just send it as a plain old javascript object ... by specifying datatype as json ... jquery will figure out how to pack the data in the request

No need to change anything on server side

So if your AJAX call becomes:

  $.ajax({
     url: 'dvsds',
     type: 'POST',
     dataType: 'json',
     data: jsondata,
      error:function(){
          alert("error occured!!!");
      },
      success:function(data){
           alert(data.message);
     }
   });

Retrieve them in the servlet as

String fname = request.getParameter("fullname");
String mobileno = request.getParameter("mobileno");

I think you will need to be careful about case sensitivity

EDITED

I see that you Can you change your script to be as follows.

<script type="text/javascript">            
function doajax(){

var fullname=$("#fullname").val;
var mobileno=$("#mobileno").val;

var postReqData = {}; // Create an empty object
postReqData['mobileno'] = mobileno;
postreqData['fullname'] = fullname;


  $.ajax({
     url: 'dvsds',
     type: 'POST',
     dataType: 'json',
     data: postReqData,
      error:function(){
          alert("error occured!!!");
      },
      success:function(data){
           alert(data.message);
     }
   });
  }

jsshah
  • 1,741
  • 1
  • 10
  • 18
  • Hi jsshah i did as mentioned but still its showing failed as valued returned in response from servlet. – shanky Jul 23 '13 at 06:42
0

The datatype attribute of the jQuery.Ajax function states the following:

dataType (default: Intelligent Guess (xml, json, script, or html))

Type: String

The type of data that you're expecting back from the server.

So, what you are sending is not a JSON string. What you are sending is plain old request data.

You can get this data in your servlet using:

String mobileno = request.getParameter("mobileno");
Community
  • 1
  • 1
MaVRoSCy
  • 17,747
  • 15
  • 82
  • 125