直接上实例:
model:这个是让SpringMVC进行参数绑定的对象
public class Json { private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
SpringMVC的controller的@RequestMapping()
@RequestMapping(value="/jsonRequestBody")//@RequestBody public String json(@RequestBody List<Json> jsons){ for(Json js:jsons){ System.out.println("name:"+js.getName()); } return "json/page"; }
jsp页面的书写(主要是JS的书写):
<hr> <input type="button" value="提交Json数据" onclick="sumbitJson()" > <script type="text/javascript"> function sumbitJson(){ var Ary=[]; var data1={"name":"希望","age":22}; var data2={"name":"梦想","age":23}; Ary.push(data1); Ary.push(data2); $.ajax({ url:‘<%=request.getContextPath() %>/json/jsonRequestBody‘, contentType:"application/json", data:JSON.stringify(Ary), type:‘post‘, dataType:‘json‘, success:function(d){ alert("success:"+d); } }); } </script>
现在问题来了:
1.如何设置,上传的数据类型?
2.如何预测服务器返回的数据类型?
时间: 2024-12-27 07:42:37