java 后台给前端传值案例(ajax jsonp跨域访问)

java代码

@RequestMapping(value = "/tobeStyleControl!prdprocess_statistics", method = {RequestMethod.POST,RequestMethod.GET}, produces = "application/json;charset=UTF-8")
    public @ResponseBody String prdprocess_statistics(HttpServletRequest request,HttpServletResponse response) throws Exception {
        log.info("tobeStyleControl!prdprocess_statistics");
              //必须定义callback  并且传递到前端 否则报错
        String callback = (String)request.getParameter("callback");
        List list_return = new ArrayList();
        try {
            Map  conditionMap = new HashMap ();
            String control_state=request.getParameter("control_state");
            conditionMap.put("control_state", control_state);
            int tenant_id=Global.getUserInfo().getTenant_id();
            conditionMap.put("tenant_id", tenant_id);
            list_return = tobeStyleControlService.prdprocess_statistics(conditionMap);
            log.info(list_return);
            String json = callback + "(" + super.getJsonUtil().toJSon(list_return).toString() + ")";
            response.setCharacterEncoding("UTF-8");
            response.getWriter().print(json);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return null;
    }

前端代码

$.ajax({
            url: localStorage.getItem('deviceurl')+'/tobeStyleControl!prdprocess_statistics.do?control_state=2',
            type:'get',
            dataType : 'jsonp',
            success:function(data){
            },
            error:function(data){
                
            }

你可能感兴趣的:(java 后台给前端传值案例(ajax jsonp跨域访问))