1

This question seems to have the answer to what I am trying to do, but it does not seem to work for me. The servlet posts fine and in the watch window I can see my json object there for the _parameters member variable of the servlet HttpRequest, but I can't seem to get the parameter out.

Here is my code.

Javascript:

// build data from input fields
var jsondata = '{"author":"TEST", "title":"XYZ"}';

$.ajax({
    type : 'POST',
    dataType : 'json',
    data: jsondata,
    url : '/submitquote',
    timeout : 5000,
    success : function(data, textStatus) {
        // whatever
    },
    error : function(xhr, textStatus, errorThrown) {
        // whatever
    }
});

Servlet (I also tried with "author" and "title" but nothing comes back):

// get data
String postData =  req.getParameter("jsondata");

This is what I see using variable watch for _parameters on the request object:

{{"author":"TEST", "title":"XYZ"}=}

How the heck do I get that stuff out?

Any help appreciated!

Community
  • 1
  • 1
JohnIdol
  • 48,899
  • 61
  • 158
  • 242

1 Answers1

4

First, the datatype argument specifies the type of the data coming out, not the data going in.

Second, the data argument should give a dictionary of parameters, and one of the parameters in this case is the already-stringified JSON object:

var jsondata = {"author":"TEST", "title":"XYZ"};

$.ajax({
    type : 'POST',
    dataType : 'json',
    data: { jsondata : JSON.stringify(jsondata)},
    url : '/submitquote',
    timeout : 5000,
    success : function(data, textStatus) {
        // whatever
    },
    error : function(xhr, textStatus, errorThrown) {
        // whatever
    }
});

Now, req.getParameter("jsondata") has the (still-JSON-stringified) data and you need to parse it yourself. JSON.org makes a very nice library you can use.

Two further notes:

  1. There are two "jsondata" here. First is the JavaScript variable, assigned in the first line and used inside the stringify call; second is the Ajax parameter name, specified right after data: { and used in the getParameter call.
  2. If you are really just passing "author" and "title", you can forget all about JSON and just use your original Javascript plus req.getParameter("author") and req.getParameter("title").
Michael Lorton
  • 43,060
  • 26
  • 103
  • 144
  • thanks! I can get the param as json now. On your note 2) you say I could use my code and query for "author" and "title", but I tried that and it doesn't seem to work, any idea why from my code? – JohnIdol Dec 26 '11 at 04:06
  • Ah, I misread your original code: there are single quotes around the object (now removed in my example) -- exactly what the stringify function should be doing for you. If you want to use JSON (the more general but more complicated solution), you should use the stringify function and not try to generate the string manual; if you want to use individual parameters (the more HTTP-y solution), take out the quotes and access the parameters directly with `getParameter()' – Michael Lorton Dec 26 '11 at 04:10
  • Thanks for all the help. I consider the question answered. I am having now problems parsing the json object ... will probably ask another q for that and post the link below. – JohnIdol Dec 26 '11 at 05:02