主要内容:
这篇文章主要是总结以前使用springmv接收json的时候遇到的问题,下面经过前台发送ajax数据后天springmvc接收,总结springmvc接收并处理ajax的问题。ajax
注意点:
一、前台发送ajax数据时必须设置的属性:
contentType='application/json'
若是不设置,后台获取到的将是url编码的文本,该属性是指定发送的数据的类型为json。
二、本文后台使用fastjson解析jsonspring
jspjson
function fun(){ $.ajax({ url:'/testAjax.action', data:"{'name':'xujie','age':'25'}", type:'POST', dataType:'json', contentType:'application/json' }) }
contentType:'application/json' //告诉服务器我发送的是json格式数据
dataType:'json',//告诉服务器,我要接收的是json格式数据数组
Controller服务器
@RequestMapping(value = "testAjax.action",method = RequestMethod.POST) public void testAjax(@RequestBody String jsonstr){ JSONObject jsonObject = JSONObject.parseObject(jsonstr);//将json字符串转换成json对象 String age = (String) jsonObject.get("age");//获取属性 System.out.println(age); }
@RequestMapping(value = "/testAjax.action",method = RequestMethod.POST) @ResponseBody public String testAjax(@RequestBody String jsonstr){ Person person = JSONObject.parseObject(jsonstr, Person.class);//将json字符串转换成json对象 System.out.println(person); return "200"; }
输出
mvc
Controllerapp
@RequestMapping(value = "testAjax.action",method = RequestMethod.POST) public void testAjax(@RequestBody String jsonstr){ JSONArray array = JSONObject.parseArray(jsonstr); JSONObject jsonObject = JSONObject.parseObject(array.get(0).toString()); String name = (String) jsonObject.get("name"); System.out.println(name); //获取到了第一个对象的name }
jspjsp
function fun(){ $.ajax({ url:'/testAjax.action', data:"[{'name':'xujie','age':'25'},{'name':'yuanxiliu','age':'20'}]", type:'POST', dataType:'json', contentType:'application/json' }) }
输出
maven
List/map
Controllerpost
@RequestMapping(value = "testAjax.action",method = RequestMethod.POST) @ResponseBody public ArrayList<String> testRequestBody() { //这里以list为例,map跟这个同样的 ArrayList<String> list = new ArrayList<String>(); list.add("apple"); list.add("orange"); list.add("pea"); return list; }
jsp
function fun(){ $.ajax({ type : "post", dataType : "json", url : "/testAjax.action", success : function(result) { alert(JSON.stringify(result)); } }); }
****要点:****
在我作这篇总结的时候,一直忘了一个事,致使使用@ResponseBody返回的时候,前台一直报错:406 (Not Acceptable) 最终发现若是使用@ResponseBody必需要添加jackson的依赖,由于springmvc在作返回的时候经过jackson去判断返回什么类型,我这里用的maven因此添加依赖:
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency>
Controller
@RequestMapping(value = "testAjax.action",method = RequestMethod.POST) public void testAjax(HttpServletResponse response){ ArrayList<String> list = new ArrayList<String>(); list.add("apple"); list.add("orange"); list.add("pea"); String jsonlist = JSON.toJSONString(list); response.getWriter().write(jsonlist); }
jsp
function fun(){ $.ajax({ url:'/testAjax.action', type:'POST', dataType:'json', contentType:'application/json', success:function(data){ alert(JSON.stringify(data)); } }) }
Controller
@RequestMapping(value = "testAjax.action",method = RequestMethod.POST) public void testAjax(HttpServletResponse response){ HashMap<String, String> map = new HashMap<String, String>(); map.put("name","xujie"); map.put("age","23"); String jsonlist = JSON.toJSONString(map); response.getWriter().write(jsonlist); }
jsp
function fun(){ $.ajax({ url:'/testAjax.action', type:'POST', dataType:'json', contentType:'application/json', success:function(data){ alert(JSON.stringify(data)); } }) }
@RequestMapping(value = "testAjax.action",method = RequestMethod.POST) @ResponseBody public void testAjax(HttpServletResponse response) throws Exception { Person person = new Person(); person.setAge("23"); person.setName("xujie"); String string = JSON.toJSONString(person); response.getWriter().write(string); }
jsp
function fun(){ $.ajax({ url:'/testAjax.action', type:'POST', dataType:'json', contentType:'application/json', success:function(data){ alert(JSON.stringify(data)); } }) }