前端html向后端java传递数据的几种方式(暂时使用到)

注:data : {},类型可按照具体场景 具体定义 不仅仅只有下面的传递方式

1 删除à前段传递方式为dataType : "JSON"、 type : "DELETE"

   前段:

$.ajax({

    url : interfaces.deleteAccessRule+id,

    type : "DELETE",

    data : {},

    dataType : "JSON",

    success : function(result) {

    },

    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]);

       }

…………………….

你可能感兴趣的:(前端html向后端java传递数据的几种方式(暂时使用到))