I have used the jQuery AJax call to send JSON as documented here in StackOverflow
The problem is that I am not receiving any Data on the server . I can see that the
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));
}
Another possible solution in wicket 6 and (maybe in wicket 5 if you use wicketAjaxPost instead of Wicket.Ajax.Post), would be to use the Wicket Ajax browser javascript library that way you can use the standard requestCycle.getRequest().getParameterMap() calls in your behavior.
You can find information about this here:
https://cwiki.apache.org/confluence/display/WICKET/Wicket+Ajax
For example instead of:
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!")
}
});
You could use:
var success = function() {alert('success!!!')};
var failure = function() {alert('failure:-(')};
// ${callbackUrl} is put in through interpolation in the renderHead of the AbstractAjaxBehavior
// TextTemplate interpolater = new PackageTextTemplate(ContentTools.class, "PackageInit.js");
// Map<String, String> variables = new HashMap<String, String>();
// variables.put("callbackUrl", getCallbackUrl());
// interpolater.interpolate(variables);
var callbackUrl = '${callbackUrl}';
var dataMap = {'name' : 'Mr Blogs', 'description' : 'a long description'};
Wicket.Ajax.post({'u': callbackUrl,
'ep': dataMap,
'sh': [success],
'fh': [failure]
});