1:自己研究的
1) 页面ajax代码
1.
2. msgObj = {"data":{"2013-11-08":3,"2013-11-07":5,"2013-11-06":3,"2013-11-05":12},"indata":{"2013-11-08":9,"2013-11-07":7,"2013-11-06":23,"2013-11-05":5}};
3. $.ajax({
4.
5.
6. type:'post',
7. '${projectPath}/update',
8. "channelType":upchannel_type,"channel":upchannel,"day":upchannel_day,"startTime":upchannel_startTime,"endTime":upchannel_endTime,"database":upchannel_db,"source":backmsgObj,"up":msgObj},//msgobj是json对象
9. 'text',//服务器返回的数据类型 可选XML ,Json jsonp script htmltext等
10. function(msg){
11. },
12. function(){
13. 'error');
14. }
15. })
1. private String channel;
2. private String channelType;
3. private String day;
4. private String startTime;
5. private String endTime;
6. private String database;
7.
8. private Map<String,Map<String,String>> source;
9. private Map<String,Map<String,String>> up; //结构与js中的json对象结构一致
1. @RequestMapping("/update")
2. public String updateBandwidth(@ModelAttribute SearchBean searchBean, HttpServletRequest request,
3. throws Exception{
4. "source :"+searchBean.getSource().toString());
5. "up :"+searchBean.getUp().toString());
6. return null;
7. }
2:在网上找的,不过试了好久没成功
在用 spring mvc 写应用的时候发现jquery传递的【json数组对象】参数后台接收不到,多订单的处理,ajax请求:
1. var cmd = {orders:[{"storeId":"0a1", "address":"西斗门路2号", "goods":[{"goodsId":"1"}, {"goodsId":"2"}, {"goodsId":"3"}]},{"storeId":"0a1", "address":"西斗门路2号", "goods":[{"goodsId":"4"}, {"goodsId":"4"}, {"goodsId":"5"}]}]}
2.
3. $.ajax({
4. url : url,
5. "POST",
6. "json",
7. data : cmd,
8. success : function(data, stats) {
9. if (stats == "success") {
10. // window.location.href="/yc"
11. }
12. },
13. error : function(data) {
14. "请求失败");
15. }
16. });
开始如何传后台用 @RequestParam 去接收如何都接不到,分析下请求头,原来是json格式没转换
1. Origin:http://localhost
2. Referer:http://localhost/test/myorder
3. User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1
4. X-Requested-With:XMLHttpRequest
5. Form Dataview URL encoded
6. orders[0][storeId]:0a1
7. orders[0][address]:西斗门路2号
8. orders[0][goods][0][goodsId]:1
9. orders[0][goods][1][goodsId]:2
10. orders[0][goods][2][goodsId]:3
11. orders[1][storeId]:0a1
12. orders[1][address]:西斗门路2号
13. orders[1][goods][0][goodsId]:4
14. orders[1][goods][1][goodsId]:4
15. orders[1][goods][2][goodsId]:5
16. Response Headersview source
17. Content-Length:1051
18. Content-Type:text/html;charset=utf-8
19. Date:Mon, 26 Nov 2012 16:10:07 GMT
20. Server:Apache-Coyote/1.1
通过观察,orders[0][storeId]:0a1
orders[0][address]:西斗门路2号
orders[0][goods][0][goodsId]:1 变成了多维数组的格式传递,没有按json对象的方式传递(原以为 jquery会自动转的,可惜我想多了,所以出现了乌龙)。
下了个jquery的json插件,jquery.json-2.4.js 转了下json;但是问题又来了,传的格式正确但是后台还是接不到,打印了下request.getParameterMap(),参数按json格式传进来,但是接的方式又出现了问题,用string接肯定不行,String数组接也接不到,List也接不到,网上查查相关Controller的接收json对象的方式,需要用@RequestBody来进行接收,为了方便干脆把 orders去掉 直接用json数组
1. var cmd = [{"storeId":"0a1", "address":"西斗门路2号", "goods":[{"goodsId":"1"}, {"goodsId":"2"}, {"goodsId":"3"}]},{"storeId":"0a1", "address":"西斗门路2号", "goods":[{"goodsId":"4"}, {"goodsId":"4"}, {"goodsId":"5"}]}]
后台用
1. (@RequestBody List orders)
这种方法来接
检查下参数写法,应该没错,测试之后还是有问题 报了这样一个错误:
POST http://localhost/test/order 415 (Unsupported Media Type)
这个问题应该是前台请求过程中没有指明contentType,ajax中加上contentType: "application/json; charset=utf-8",再测试,果然可以了。完整代码如下:
ajax:
1. var cmd = [{"storeId":"0a1", "address":"西斗门路2号", "goods":[{"goodsId":"1"}, {"goodsId":"2"}, {"goodsId":"3"}]},{"storeId":"0a1", "address":"西斗门路2号", "goods":[{"goodsId":"4"}, {"goodsId":"4"}, {"goodsId":"5"}]}]
2.
3. $.ajax({
4. url : url,
5. "POST",
6. "json",
7. "application/json; charset=utf-8",
8. data : $toJSON(cmd),
9. function(data, stats) {
10. if (stats == "success") {
11. // window.location.href="/yc"
12. }
13. },
14. function(data) {
15. "请求失败");
16. }
17. });
Controller method:
1. @RequestMapping(value = "/order", method = RequestMethod.POST)
2. public ModelAndView order(@RequestBody List<Map<String,Object>> orders) {
3. "orders size:" + orders.size());
4. }
以上希望能为遇到类似问题的朋友提供一些帮助。