springMVC中ajax数组传值报400或者415错误

错误原因

400 请求出错 
由于语法格式有误,服务器无法理解此请求。不作修改,客户程序就无法重复此请求。 
415  
介质类型不受支持 — 服务器拒绝服务请求,因为不支持请求实体的格式。  
检查 data 吧,看看提交到后台的数据是否合法!



这是由于在后台服务端定义的参数与传送的格式对应不上引起的。



解决方法

下面给出正确的使用方式:

jsp文件中

[java]  view plain  copy
  1. var pointCodes= new Array(); //定义一数组   
  2.         pointCodes=$('#pointCodes').val().trim().split(',');  
  3.         $.ajax({  
  4.             url:'/primer/bind-primer',  
  5.             type:"POST",  
  6.             data:{"pointCodes":pointCodes,"id":$('#primerId').val()},  
  7.             timeout:3000000,  
  8.             dataType:"json",  
  9.             success:function(res){     alert('成功');}, error:function(){ alert('服务器忙,请稍后再试'); } });  
 
 

我们在这里的data传递的是一个json文档,里面有数组pointCodes,有id值。

后台接受时如下:

java文件中

[plain]  view plain  copy
  1. @RequestMapping("/bind-primer")  
  2.  @ResponseBody  
  3.  public AjaxResult bindPrimer(@RequestParam(value = "pointCodes[]") String[] pointCodes,@RequestParam String id) {  
  4.      return AjaxResult.resultSuccess(primerService.bindPrimer(pointCodes, id));  
  5.  }  

这里的关键在于

SpringMVC传递一维数组:传递数组类型时,需要在@requestParam()中添加value。

猜你喜欢

转载自blog.csdn.net/li7134600/article/details/77854462