jquery - JSON getting parameter as null -


i using ajax , json pass array servlet. using struts1.3 framework. receiving null parameter when parameter in actionservlet.

    $.ajax({     url: "starttest.do?cmdfield=ajax",     type: "post",     contenttype: "application/json; charset=utf-8",     datatype: "json",     data:json.stringify(answerjson),     contenttype:"application/json"     //complete:callback });  

my json object answerjson value :

 {"answer":[{"qid":"13","selans":"a"},{"qid":"2","selans":"a"},{"qid":"12","selans":"a"},{"qid":"6","selans":"a"}]} 

in servlet, if use below statement gives null.

system.out.println(request.getparameter("answer")); 

i use tried use gson as

jsonans ans = gson.fromjson(request.getparameter("answer"), jsonans.class); 

which in turn gave me error.

how can parameter values list using gson?

i found solution. req.getparameter("anything") assumes content type of "application/x-www-form-urlencoded" or normal http (for url parameters).

when content type "application/json", need use:

 foo foo = gson.fromjson(req.getreader(), foo.class); 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -