How server-side get json from client?
I create a jsp file and use struts. In my jsp I use below code.
- $("#myForm").submit(function(){
- $.post(
- "./MyAction.do?cmd=updateDb",
- { name:'panda', age:'23' },
- function(text,state) { alert ("StatusCode:"+state ); },
- "application/json"
- );
- return false
- });
When myForm is submit. jsp send json to server. I want to know what server-side should do to get the json.. I try this code but not work. It tell 'reader.readline() is null'
- public class myAction extends DispatchAction{
- public ActionForward updateDb (ActionMapping mapping,
- ActionForm form,
- HttpServletRequest request,
- HttpServletResponse response) throws Exception {
- String jsonString = readJSONStringFromRequestBody(request);
- }
- private String readJSONStringFromRequestBody(HttpServletRequest request) throws Exception{
- StringBuffer json = new StringBuffer();
- String line = null;
-
- try{
- BufferedReader reader = request.getReader();
- while((line=reader.readLine()) != null ){
- json.append(line);
- }
- }catch(Exception e){
- throw new Exception("Error Reading JSON string:"+e.toString());
- }
- return "";
- }
- }
Anyone please help me. sorry for my english.