Using Wicket AbstractAjaxBehavior with jQuery.ajax()

前端 未结 2 2024
别那么骄傲
别那么骄傲 2020-12-31 23:03

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

相关标签:
2条回答
  • 2020-12-31 23:32

    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));
            }
    
    0 讨论(0)
  • 2020-12-31 23:37

    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]
       });
    
    0 讨论(0)
提交回复
热议问题