How to handle jQuery.ajax(..) JSON request ?

 Apache Wicket > Framework Documentation > Reference library > How to do things in Wicket > AJAX > How to handle jQuery.ajax(..) JSON request ?
#editReport()

The following is the jQuery JSON request code

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

console.log(" call back url :"+ callBackURL);
               $.ajax({
                    url: callBackURL,
                    type: 'post',
                    cache: false,

                    data:JSON.stringify(ccbArry),
                    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);
                    }
                    

                });

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. 

        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()){
                               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));
            }


        };
        add(ajaxSaveBehaviour);
       
        String callBackURL= ajaxSaveBehaviour.getCallbackUrl().toString();
        logger.info(" callback url :"+ callBackURL);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值