注:data : {},類型可按照具體場景 具體定義 不單單隻有下面的傳遞方式java
1 刪除à前段傳遞方式爲dataType : "JSON"、 type : "DELETE"web
前段:ajax
$.ajax({json
url : interfaces.deleteAccessRule+id,bootstrap
type : "DELETE",後端
data : {},數組
dataType : "JSON",app
success : function(result) {dom
},url
error : function() {
alert("刪除錯誤!");
}
});
後端java:
@RequestMapping(value="/meta/v1/accessRules/{id}",method=RequestMethod.DELETE, produces = "application/json;charset=UTF-8")
@ResponseBody
public boolean delete(@PathVariable(value = "id") Integer id){
boolean result = accessRuleService.delete(id);
return result;
}
2 增長à前段傳遞方式爲dataType:"JSON"、 type:" POST" 以form的方式傳送數據
前段:
$.ajax({
url : interfaces.registerAccessRule,
type : "POST",
data : $("#addaccessruleInfo").serialize(),
dataType : "JSON",
success : function(result) {
},
error : function() {
}
});
後端java:
@RequestMapping(value="/meta/v1/accessRules",method=RequestMethod.POST, produces = "application/json;charset=UTF-8")
@ResponseBody
public Map<String,Object> register(HttpServletRequest request){
Map<String,Object> map = new HashMap<String, Object>();
map.put("objectId", request.getParameter("addobjectid"));
return accessRuleService.register(map);
}
3 查詢à前段傳遞方式爲dataType:"JSON"、 type:"GET"
前段:
$.ajax({
url : interfaces.searchByDomainId +id,
type : "GET",
data : {},
dataType : "JSON",
success : function(result) {
},
error : function() {
}
});
後端java:
@RequestMapping(value = "/meta/v1/domaincategories/{domaincategoryId}/domains", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
@ResponseBody
public List<Map<String, Object>> searchBydomaincategoryId(@PathVariable("domaincategoryId") Integer domaincategoryId) {
List<Map<String, Object>> domainCategoryList = domainService.searchBydomaincategoryId(domaincategoryId);
return domainCategoryList;
}
4 修改à前段傳遞方式爲dataType:"JSON"、 type:"PUT"以form的方式傳送數據
前段:
$.ajax({
url : interfaces.alterAccessRule + accessRuleId,
type : "PUT",
data : $("#editaccessruleInfo").serialize(),
dataType : "JSON",
success : function(result) {
},
error : function() {
}
});
後端java:
@RequestMapping(value="/meta/v1/accessRules/{id}",method=RequestMethod.PUT, produces = "application/json;charset=UTF-8")
@ResponseBody
public Map<String,Object> alter(HttpServletRequest request){
Map<String,Object> map1 = new HashMap<String, Object>();
map1.put("id", request.getParameter("editid"));
map1 = accessRuleService.alter(map1);
return map1;
}
5 傳值à前段傳遞方式爲dataType:"JSON"、 type:" POST " 傳送串
前段:
Var Temp = $.map($table.bootstrapTable('getSelections'), function (row) {return row.id;});
$.ajax({
url : interfaces.addportalportlet+portalId,
type : "POST",
data :{w1:JSON.stringify(temp)},
dataType : "JSON",
success : function(result) {
if(result!=null){
alert("新增成功!");
refreshTable();
}else{
alert("新增失敗!");
}
},
error : function() {
alert("新增失敗!");
}
});
後端java:
@RequestMapping(value="/meta/v1/portalportlets/{portalId}",method=RequestMethod.POST, produces = "application/json;charset=UTF-8")
@ResponseBody
public List<Map<String,Object>> register(HttpServletRequest request,@PathVariable("portalId") Integer portalId){
String ss=request.getParameter("w1");
String b=ss.replace("[","").replace("]","");
String[] arr = b.split(",");
List<Map<String,Object>> paramList=new ArrayList<Map<String, Object>>();
for(int i=0;i<arr.length;i++){
Map<String,Object> map=new HashMap<String,Object>();
map.put("portletId",arr[i]);
map.put("portalId",portalId);
paramList.add(map);
}
List<Map<String,Object>> resultmapList=portalPortletService.register(paramList);
return resultmapList;
}
5 傳數組à前段傳遞方式爲dataType:"JSON"、 type:" POST " 傳數組
前段:
var _list = [];
for(var i=0 ; i<10 ; i++){
_list[i] = "abc"+i;
}
$.ajax({
url : interfaces.addCertifications,
type : "POST",
data : {"aa":_list},
dataType : "JSON",
traditional:true,//如以HttpServletRequest接受須要寫
success : function(result) {
},
error : function() {
}
});
後端java:
@RequestMapping(value = "/cust/v1/certifications", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
@ResponseBody
public Map<String, Object> register(HttpServletRequest request) {
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < request.getParameterValues("aa").length; i++) {
System.out.println(request.getParameterValues("aa")[i]);
}
…………………….