Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

The

...

following

...

is

...

the

...

jQuery

...

JSON

...

request code

This section of code invokes a abstract behavior ona page with a json request:

Code Block


 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) {
                        console.log(" reponse :"+ json);

                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                            console.log("error :"+XMLHttpRequest.responseText);
                    }
                    

                });



// the follwoing code handles ajax json request from jQuery

   

This section of code  is an abstract ajax behavior which accepts a json document in request and responds with another json  document that is then available to the  javascript method on the page. 

Code Block


        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                 
                           String  jsonString = br.readLine();
                                                     if((jsonString==null) || jsonString.isEmpty()){
                                                              logger.error(" no json found");
                           }
                           else {
                                                         }
                           else {
                               logger.info(" jsonjson   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));
                        }


               };
               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();
        logger.info(" callback url :"+ callBackURL);