1,json传入参数
java代码:
/**
* 插入服务器文件
* @param linuxFile
* @return
*/
@RequestMapping(value = "/insert", method = RequestMethod.POST)
@ResponseBody
public DataTransferObject insert(@RequestBody LinuxFile linuxFile,HttpServletRequest request) {
return linuxFileProxy.insert(linuxFile);
}
前端js代码:
var param = {};
param.settingsId = settingsId;
param.filePath = filePath;
$.ajax({
url: '/ueimanager/manager/linux/linuxFile/insert',
type: 'POST',
contentType: 'application/json;charset=UTF-8',
dataType: 'json',
data: $.toJSON(param),
success: function(data) {
if (data == 1) {
initPageList(settingsId);
} else {
alert("新增失败!");
}
}
});
注意:contentType: 'application/json;charset=UTF-8',不能少。
2,分页方法传入参数(java对象不需要@RequestBody LinuxFile linuxFile注解)
java代码:
@Autowired
private LinuxFileProxy linuxFileProxy;
/**
* 获取服务器文件的分页信息
* @param linuxFile
* @return
*/
@RequestMapping(value = "/page", method = RequestMethod.GET)
@ResponseBody
public DataTransferObject page(LinuxFileQO linuxFileQO, HttpServletRequest request) {
return linuxFileProxy.page(linuxFileQO);
}
前端js代码:
var param = {};
param.pageNo = 1;
param.pageSize = 100;
param.settingsId = settingsId;
$.ajax({
url: '/ueimanager/manager/linux/linuxFile/list4Linux',
type: 'GET',
dataType: 'json',
data: param,
success: function(data) {
$("#tb").empty();
if (data.resultList != null) {
currentrecord = data.resultList.length;
lastpage = Math.floor(data.sum / pageSize);
for (var i = 0; i < data.resultList.length; i++) {
var id = data.resultList[i].id;
var filePath = data.resultList[i].filePath;
data.resultList[i].appendbtn = '<a class="btn mini purple" href="javascript:void(0);" οnclick="appendbtn(' + id + ',\'' + filePath + '\',' + settingsId + ')" >备注</a>';
data.resultList[i].shellbtn = '<a class="btn mini black" href="javascript:void(0);" οnclick="del(' + id + ',' + settingsId + ')" >shell</a>';
}
$("#hiddenTemplateDOM").tmpl(data.resultList).appendTo("#tb");
}
}
});
注意:contentType: 'application/json;charset=UTF-8',不需要。