0

this is my javascript code to send json object to servlet

username = "Nash";
password = "619here";
type = "all";
data = '{ "username": "' + username + '", "password": "' + password + '", "type": "' + type + '" }';
request = JSON.parse(data);

$.getJSON(url, request, function (response) {
    $("#result").append("<h1>Success</h1>");
    $.each(response.vehicle, function (no, vehicle) {

        $.each(vehicle, function (key, value) {

            $("#result").append("<h2>" + key + " : " + value + "</h2>");

        });

        $("#result").append("<br>");
    });

})

I want read json object from java servlet and get data separately. like this

String username = username from json object
String password = password from json object
String type = type from json object

I'm using json-lib-2.4-jdk15.jar please help me..

Lion
  • 18,729
  • 22
  • 80
  • 110
heshan
  • 115
  • 1
  • 4
  • 8

1 Answers1

0

In your servlet, do the following in the do[Get,Post]() method

JSONObject jsonObject = JSONObject.fromObject( request.getQueryString() );  

String username= jsonObject.get( "username" );  
String password= jsonObject.get( "password" ); 
String type= jsonObject.get( "type" ); 

You also need the remove the following line from your JS.

request = JSON.parse(data);
Pradeep Pati
  • 5,779
  • 3
  • 29
  • 43
  • it gives this exception.. net.sf.json.JSONException: A JSONObject text must begin with '{' at character 1 of callback=jQuery19009972247087862343_1370721676614&username=Nash&password=619here&type=all&_=1370721676615 – heshan Jun 08 '13 at 20:15