跨域调用接口代码

   @RequestMapping("/login")
    @ResponseBody
    public void login(HttpServletRequest request, HttpServletResponse response,String params){
        AjaxResult result = new AjaxResult();
        String client_id="";
        response.setHeader("Access-Control-Allow-Origin", "*");  
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");  
        response.setHeader("Access-Control-Max-Age", "3600");  
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
        response.setHeader("Content-type", "text/html;charset=UTF-8");//解决访问的乱码
        PrintWriter pw = null;
        JSONObject jsonObj=new JSONObject();
        try {
            pw=response.getWriter();
            //用户登录标识 存在且不为空表示 用户已经登录
            String TYSFRZ_TGC = null; 
            //将参数转化成json字符串
            JSONObject paramsJson = JSONObject.fromObject(params);
            //paramsJson 为空,说明传入参数名不对或者参数值不对
            if (paramsJson ==null || paramsJson.isNullObject()) {
                jsonObj.put("code", JxUiasCodeDefine.CODE_ERROR);
                jsonObj.put("data", "");
                jsonObj.put("msg", "请传入参数名为params,值为json格式的字符串的参数!");
                jsonObj.put("success", false);
                pw.print(jsonObj.toString());
                return;
            }
            String data = MCommonUtil.createTicket(TYSFRZ_TGC, authapplicationdo.getLoginout_url());//生成临时票据
            jsonObj.put("code", JxUiasCodeDefine.CODE_SUCCESS);
            jsonObj.put("data", data);
            jsonObj.put("msg", "已经登录,无需再次登录");
            jsonObj.put("success", true);
            AuthAccessLog accessLog = new AuthAccessLog(client_id,new Date(),"login","PC端用户登录",result.getCode());
            saveAccessInfo(accessLog);
            pw.print(jsonObj.toString());
            return;
        } catch (Exception e) {
            log.error(this, e);
            result.setCode(JxUiasCodeDefine.CODE_ERROR);
            result.setData("");
            result.setMsg("操作失败");
            if(StringUtils.isNotBlank(client_id)){
                AuthAccessLog accessLog = new AuthAccessLog(client_id,new Date(),"login","PC端用户登录",result.getCode());
                saveAccessInfo(accessLog);
            }
        }
        return;
    }
上一篇:java—TCP


下一篇:url详解