我已经使用jQuery AJax调用来发送
JSON,如
StackOverflow中所述
问题是我没有收到服务器上的任何数据.我可以看到调用确实达到了目标ajax行为 – 但在onRequest()方法中,RequestCycle dd不包含任何参数
我的Wicket代码:
AbstractAjaxBehavior ajaxSaveBehavIoUr = new AbstractAjaxBehavior(){ private static final long serialVersionUID = 1L; @SuppressWarnings("unchecked") public void onRequest() { //get parameters final RequestCycle requestCycle = RequestCycle.get(); final PageParameters pageParameters = new PageParameters(requestCycle.getRequest().getParameterMap()); logger.info(" I have received something 1"); for(String pkey: requestCycle.getRequest().getParameterMap().keySet()){ String[] valArry= requestCycle.getRequest().getParameterMap().get(pkey); StringBuffer sb = new StringBuffer(); for(String s: valArry) sb.append(s).append(","); logger.info("pk :"+ pkey + " = "+ sb.toString()); } //do something using nice json library to produce a string of json logger.info(" I have received something 2"); for(String key: pageParameters.keySet()){ Object o= pageParameters.get(key); logger.info("received key : "+ key + " = " +o.toString()); } String data="ok"; requestCycle.setRequestTarget(new StringRequestTarget("application/json","utf-8",data)); } }; add(ajaxSaveBehavIoUr); String callBackURL= ajaxSaveBehavIoUr.getCallbackUrl().toString();
console.log(" call back url :"+ callBackURL); $.ajax({ url: callBackURL,type: 'post',cache: false,data:JSON.stringify(ccbArry[0]),contentType: 'application/json',dataType: 'json',complete: function() { alert(" completed okey dokey!") } });
从我的Firebug控制台,我可以看到JSON POST成功完成并且警报(“完成的okey dokey!”)dos被触发.
问题是在Wicket上,AbstractAjaxBehavior无法在RequestCycle中找到任何参数.
有什么我想念的吗?有趣的是,我运行这是调试器,因为我找不到任何参数.这看起来像编码问题.
从Firebug,我可以看到这是发出的电话
http://localhost:8080/mywebapp-web-1.0-SNAPSHOT/?wicket:interface=:0::IActivePageBehaviorListener:0:&wicket:ignoreIfNotActive=true&%7B%22type%22%3A9504%2C%22sourceNewsClipBean%22%3A%7B%22type%22%3A9503%2C%22id%22%3A%224cf05752acc1d6aebface86d%22%2C%22typeString%22%3A%22NEWSCLIP_TYPE%22%7D%2C%22startOffset%22%3A%22195%22%2C%22clipDuration%22%3A%22297%22%7D=
不知何故,这些参数在RequestCycle中没有出现.它看起来像一个狡猾的问题.有任何想法吗 ?
解决方法
好吧,我找到了解决方案:关键是不使用requestCycle.getRequest().getParameterMap()从浏览器中读取JSON.而是直接从servlet输入流中读取数据,如下所示:
有用 .
有用 .
public void onRequest() { //get parameters final RequestCycle requestCycle = RequestCycle.get(); WebRequest wr=(WebRequest)requestCycle.getRequest(); HttpServletRequest hsr= wr.getHttpServletRequest() ; try { BufferedReader br = hsr.getReader(); String jsonString = br.readLine(); if((jsonString==null) || jsonString.isEmpty()){ logger.error(" no json found"); } else { logger.info(" json is :"+ jsonString); } } catch (IOException ex) { logger.error(ex); } // json string to retir to the jQuery onSuccess function String data=getReturnJSONValue(); logger.info("returning json :"+ data); IRequestTarget t = new StringRequestTarget("application/json","UTF-8",data); getRequestCycle().setRequestTarget(t); //requestCycle.setRequestTarget(new StringRequestTarget("application/json",data)); }