zoukankan      html  css  js  c++  java
  • 跨域调用接口代码

       @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;
        }
  • 相关阅读:
    nodejs之express路由与动态路由
    nodejs之mongodb操作
    nodejs之路由
    nodejs之简单应用与运行
    nodejs之fs 模块
    nodejs 之简单web服务器
    C++之结构体struct
    STL之stack
    【剑指offer】05替换空格,C++实现
    【剑指offer】04A二维数组中的查找,C++实现
  • 原文地址:https://www.cnblogs.com/jassy/p/10372671.html
Copyright © 2011-2022 走看看