Using Wicket AbstractAjaxBehavior with jQuery.ajax()

倖福魔咒の 提交于 2019-11-30 09:27:04

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]
   });
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!