1. 提交简单的JSON数据
js
var params = {classCode:"fuck"};
$.ajax({
type: "POST",
url: "http://localhost/public/Save.action",
dataType: "json",
data: params, // Post 方式,data参数不能为空"",如果不传参数,也要写成"{}",否则contentType将不能附加在Request Headers中。
success: function(){alert(1)},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown + ':' + textStatus); // 错误处理
}
});
action:
View Code
String classCode; public void setClassCode(String classCode) { this.classCode = classCode; }
2. 提交JSON数组
js:
var categoryJson = "{categoryJson:[ {'id' : 1, 'name' : '类型1', 'level' : 1}, {'id' : 2, 'name' : '类型2' , 'level' : 1} ]}";
$.ajax({
type: "POST",
url: "http://localhost/public/Save.action",
dataType: "json",
data : "myJson=" + categoryJson,//
success: function(){alert(1)},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown + ':' + textStatus); // 错误处理
}
});
action 要 implements ParameterAware
View Code
private Map parameters; //接收参数 public void setParameters(Map parameters) { this.parameters = parameters; } public String save() { JSONObject json = new JSONObject(); try { String[] params1 = (String[]) parameters.get("myJson");//接收myJson参数 String jsonStr = new String(params1[0]); JSONObject jsonobj = new JSONObject(jsonStr); JSONArray jsonArray = jsonobj.getJSONArray("categoryJson");//json数组对应的键 int a = 1; } catch (Exception e) { } finally { } return SUCCESS; }

浙公网安备 33010602011771号