JS中的变量,怎么通过ajax在controller中取值后,再赋值

参考链接:

https://www.cnblogs.com/Relict/p/3893963.html

 

举例:

    @RequestMapping(value = "/getLanguage", method = RequestMethod.GET)
    public @ResponseBody
    JSONObject getLanguage(ModelMap modelMap,
                                      HttpSession httpSession,
                                      HttpServletRequest request,
                                      HttpServletResponse response) throws Exception {
        // 先得到当前语言,为空则默认为中文,1:中文 2:英文
        int currentLanguage = 1;
        if ((Integer) httpSession.getAttribute("lang") == null) {
            currentLanguage = 1;
        } else {
            currentLanguage = (Integer) httpSession.getAttribute("lang");
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("lang", currentLanguage);
        return jsonObject;
    }
            var temp=1;
            $.ajax({
                type:"GET",
                dataType: 'json',
                url:"/getLanguage",
                async: false, // 必须为false
                success:function(data){
                    console.log('data.lang is ' + data.lang);
                    if(data.lang==1){ // 中文
                        temp=1;
                    }else{
                        temp=2;
                    }
                },
                error: function (result) {
                    temp=1;
                }
            });

 

你可能感兴趣的:(java,web,JavaScript)