SpringMVC返回json数据+解决中文乱码

时间:2021-07-21 20:24:33

使用ajax来发送get请求到springMVC后台,后台返回json数据

JSP代码:

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script>
function createAjaxObj(){
var req;
if(window.XMLHttpRequest){
req = new XMLHttpRequest();
}else{
req = new ActiveXObject("Msxml2.XMLHTTP"); //ie
}
return req;
}

function sendAjaxReq(){
var req = createAjaxObj();
req.open("get","myajax.do?method=test2&uname=张三zhang&a="+Math.random());
req.setRequestHeader("accept","application/json");
req.onreadystatechange = function(){
// 获取服务器的响应结束
var text = req.responseText;
// 获取h1元素
var h1 = document.getElementById("h1");
h1.innerHTML = text;

eval("var result="+req.responseText);
document.getElementById("div1").innerHTML=result.user.uname;
};
req.send(null);
}
</script>
</head>

<body>
<a href="javascript:void(0);" onclick="sendAjaxReq();">测试</a>
<div id="div1"></div>

<h1 id="h1"></h1>
</body>
</html>

SpringMVC后台代码:

package com.sxt.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sxt.po.User;

@Controller
@RequestMapping("myajax.do")
public class MyAjaxController {

@ResponseBody //设置返回参数为json数据
@RequestMapping(params="method=test2",method=RequestMethod.GET)
public Map<String,Object> test2(String uname,HttpServletResponse response
,HttpServletRequest request) throws Exception{
//处理响应编码问题
response.setHeader("Content-Type", "text/html;charset=utf-8");
// request.setCharacterEncoding("utf-8"); //只对post请求有效
//设置get请求的编码参数,如果是post请求只需request.setCharacterEncoding("utf-8")
String uname2 = new String(uname.getBytes("iso8859-1"),"utf-8");
System.out.println(uname2);
Map<String,Object> map = new HashMap<String,Object>();
map.put("name", "老张");
map.put("user", new User("潇洒哥","22"));
return map;
}
}

注意:中文乱码问题

1、get请求中含有中文时,后台获取get传递的参数时要这样获取:String uname2 = new String(uname.getBytes("iso8859-1"),"utf-8");

      如果是post请求,则在获取参数之前直接加上request.setCharacterEncoding("utf-8");      即可解决问题

2、后台返回数据包含中文(get与post处理方法是一样的是一样的)

      在返回数据之前加上  response.setHeader("Content-Type", "text/html;charset=utf-8");     即可解决问题

      此时前端接受的数据为utf-8