Maven+SpringMVC+Mybatis+ajax:后台传值到前台中文出现问号乱码问题

Jquery代码:

$.ajax({
		type: 'POST',
		url: 'user/login',
		data: JSON.stringify(datas),
		contentType: 'application/json',
		success: function(data){
			if('' != data && null != data)
				alert(data);
			else
				alert('请检查你的账号和密码是否正确...');
		}
	});

web.xml代码:

  
    
    encodingFilter  
    org.springframework.web.filter.CharacterEncodingFilter  
    
        encoding
        UTF-8
    
     
        forceEncoding
        true
    
    
    
    encodingFilter  
    /*  
    

spring-servlet.xml代码:


	
     	
           
               
                   
                       application/json;charset=UTF-8
                       text/html;charset=UTF-8
                   
               
           
       	
     

Controller代码:

@RequestMapping(value = "login", method = RequestMethod.POST)
	@ResponseBody
	public String login(@RequestBody String data,HttpServletResponse response){
		UserInfo user = gson.fromJson(data, UserInfo.class);
		map.put("loginname", user.getLoginname());
		map.put("pwd", user.getPwd());
		user = service.login(map);
		response.setCharacterEncoding("UTF-8");
                response.setContentType("text/html;charset=UTF-8");
		if(null != user)
			return gson.toJson(user);
		return null;
		
	}
HttpServletResponse response){
		UserInfo user = gson.fromJson(data, UserInfo.class);
		map.put("loginname", user.getLoginname());
		map.put("pwd", user.getPwd());
		user = service.login(map);
		response.setCharacterEncoding("UTF-8");
                response.setContentType("text/html;charset=UTF-8");
		if(null != user)
			return gson.toJson(user);
		return null;
		
	}

OK,彻底解决

 

 

 

 

 

你可能感兴趣的:(后端)