Maven+SpringMVC+Mybatis+ajax: There is a question mark garbled problem in Chinese when the value is passed from the background to the front desk

Jquery code:
$.ajax({
		type: 'POST',
		url: 'user/login',
		data: JSON.stringify(datas),
		contentType: 'application/json',
		success: function(data){
			if('' != data && null != data)
				alert(data);
			else
				alert('Please check if your account and password are correct...');
		}
	});

web.xml code:

<!-- character filter -->  
  <filter>  
    <filter-name>encodingFilter</filter-name>  
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
    <init-param><!-- for request -->
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param><!-- for response -->
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
  </filter>  
  <filter-mapping>  
    <filter-name>encodingFilter</filter-name>  
    <url-pattern>/*</url-pattern>  
  </filter-mapping>  


spring-servlet.xml code:

<!-- Configure automatic scanning: solve the problem of Chinese garbled characters-->
	<mvc:annotation-driven>
     	<mvc:message-converters>
           <bean class="org.springframework.http.converter.StringHttpMessageConverter">
               <property name="supportedMediaTypes">
                   <list>
                       <value>application/json;charset=UTF-8</value>
                       <value>text/html;charset=UTF-8</value>
                   </list>
               </property>
           </bean>
       	</mvc:message-converters>
     </mvc:annotation-driven>
Controller code:

@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;
		
	}
OK, completely solved

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325809065&siteId=291194637