将Wicket AbstractAjaxBehavior与jQuery.ajax()结合使用 [英] Using Wicket AbstractAjaxBehavior with jQuery.ajax()
问题描述
I have used the jQuery AJax call to send JSON as documented here in StackOverflow
问题是我没有在服务器上收到任何数据.我可以看到该调用确实达到了目标ajax行为-但在onRequest()方法中,RequestCycle dd不包含任何参数
The problem is that I am not receiving any Data on the server . I can see that the call did reach the target ajax behavior -- but in onRequest() method , the RequestCycle dd not contain any parameters
我的票务代码:
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();
我的Javascript调用此方法
My Javascript that invokes this method
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,并且触发了alert("completed okey dokey!")dos.
From my Firebug console, I can see that the JSON POST was made succesfully and the alert(" completed okey dokey!") dos get triggered.
问题是在Wicket上AbstractAjaxBehavior无法在RequestCycle中找到任何参数.
The problem is that on the Wicket AbstractAjaxBehavior is unable to find any parameters in the RequestCycle.
有什么我想念的吗?有趣的是,我运行的是调试器,因为我找不到任何参数.这看起来像是一个编码问题.
Is there something I am missing ? The funny thing is that I ran this is debugger asn I could not find any parameter. This looks like an encoding issue.
从Firebug中,我可以看到这是发出的呼叫
From Firebug, I could see that this was the call that was made
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中.看起来像一个问题.有什么想法吗?
Somehow , these parameters do not apperd in the RequestCycle. It looks like an encodong issue . Any ideas ?
推荐答案
好,我找到了解决方案:关键是不要使用requestCycle.getRequest().getParameterMap()从浏览器读取JSON.而是直接从servlet输入流中读取数据,如下所示: 可以.
ok I found the solution : The keys was to not use requestCycle.getRequest().getParameterMap() to read the JSON from the browser. Instead read the data directly from the servlet input stream as below: It works .
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", "utf-8", data));
}
这篇关于将Wicket AbstractAjaxBehavior与jQuery.ajax()结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!