Json对象
var params = {"id": 1,"name": "测试商品","price": 99.9,"detail": "测试商品描述","pic": "123456.jpg"};
Json串
var params = '{"id": 1,"name": "测试商品","price": 99.9,"detail": "测试商品描述","pic": "123456.jpg"}';
提交代码
$.ajax({
url :"${pageContext.request.contextPath}/item/json.action",
data : params,
//contentType : "application/json;charset=UTF-8",//发送数据的格式
type : "post",
dataType : "json",
success : function(data){
alert(data);
}
});
当采用ajax提交时,若提交的数据填写json对象,则会转换成键值对的形式提交
而若填写json串,则不会转换成键值对
在springmvc中,提交json串,并添加下面代码
contentType : "application/json;charset=UTF-8",//发送数据的格式
则在Controller中可以解析该字符串并封装到对象,此时传输的数据在
request payload
// json数据交互
@RequestMapping(value = "/json.action")
public @ResponseBody Items json(@RequestBody Items items){
System.out.println(items);
return items;
}