You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

The following is the jQuery JSON request code
// make sure that callBackURL is a javascript variable set from ajaxSaveBehaviour.getCallbackUrl().toString();
//
console.log(" call back url :"+ callBackURL);
$.ajax({
url: callBackURL,
type: 'post',
cache: false,

data:JSON.stringify(ccbArry), // ccbArry is my json array
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function(json)

Unknown macro: { console.log(" reponse }

,
error: function (XMLHttpRequest, textStatus, errorThrown)

Unknown macro: { console.log("error }

});

// the follwoing code handles ajax json request from jQuery

AbstractAjaxBehavior ajaxSaveBehaviour = new AbstractAjaxBehavior(){
private static final long serialVersionUID = 1L;

@SuppressWarnings("unchecked")
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())

Unknown macro: { logger.error(" no json found"); }

else

Unknown macro: { logger.info(" json is }

} catch (IOException ex)

Unknown macro: { 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));
}

};
add(ajaxSaveBehaviour);

// make sure that you pass this url as a javascript variable to the page that will make the ajax json request
String callBackURL= ajaxSaveBehaviour.getCallbackUrl().toString();

  • No labels