springMVC接收前端參數的方式

1.springMVC接收前端一個數組(紅色標註的地方,都是須要注意的地方)前端

方法一(使用json字符串的形式傳到後臺):ajax

var addCompanyIDs=[1,2,3,4];
$.ajax({
        async:true,
        cache: true,
        type: "POST",
        contentType: "application/json;charset=UTF-8",
        url:"",
        data:JSON.stringify(addCompanyIDs),
        error: function(request) {
               
        },
        success: function(result){
                
        }
     });
@RequestMapping(path = "/saveCompanyfocus", method = RequestMethod.POST)
    public @ResponseBody boolean saveCompanyfocus(@RequestBody List<String> addCompanyIDs) {
        return true;
    }

方法二 (以字符串的形式提交)spring

 
 
var addCompanyIDs=[1,2,3,4];
$.ajax({
        async:true,
        cache: true,
        type: "POST",
        url:"",
        data: {
            addCompanyIDs:addCompanyIDs.toString()
        }, 
        error: function(request) {
           
        },
        success: function(result){
           
        }
     });
@RequestMapping(path = "/saveCompanyfocus", method = RequestMethod.POST)
    public @ResponseBody boolean saveCompanyfocus(@RequestParam List<String> addCompanyIDs) {
        return true;
    }

方法三(以數組的形式提交)json

var addCompanyIDs=[1,2,3,4];
$.ajax({
        async:true,
        cache: true,
        type: "POST",
        url:"",
        data: {
            addCompanyIDs:addCompanyIDs
        }, 
        error: function(request) {
               
        },
        success: function(result){
                
        }
     });
@RequestMapping(path = "/saveCompanyfocus", method = RequestMethod.POST)
    public @ResponseBody boolean saveCompanyfocus(@RequestParam(value="addCompanyIDs[]",required=false) String[] addCompanyIDs) {
        return true;
    }

2.springMVC接收前端多個數組(紅色標註的地方,都是須要注意的地方)數組

方法一(以數組的形式提交):app

var addCompanyIDs=[1,2,3,4];
var delCompanyIDs=[5,6,7];
$.ajax({
        async:true,
        cache: true,
        type: "POST",
        url:"saveCompanyfocus",
        data: {
          delCompanyIDs:delCompanyIDs, addCompanyIDs:addCompanyIDs
        }, 
        error: function(request) {
               
        },
        success: function(result){
               
        }
     });
@RequestMapping(path = "/saveCompanyfocus", method = RequestMethod.POST)
    public @ResponseBody boolean saveCompanyfocus(@RequestParam(value="delCompanyIDs[]",required=false) String[] delCompanyIDs,@RequestParam(value="addCompanyIDs[]",required=false) String[] addCompanyIDs) {
        return true;
    }    

方法二(以字符串的形式提交)async

var addCompanyIDs=[1,2,3,4];
var delCompanyIDs=[5,6,7];
$.ajax({
        async:true,
        cache: true,
        type: "POST",
        url:"",
        data: {
            delCompanyIDs:delCompanyIDs.toString(), addCompanyIDs:addCompanyIDs.toString()
        }, 
        error: function(request) {
        },
        success: function(result){
               
        }
     });
@RequestMapping(path = "/saveCompanyfocus", method = RequestMethod.POST)
    public @ResponseBody boolean saveCompanyfocus(@RequestParam List<String> delCompanyIDs,@RequestParam List<String> addCompanyIDs) {
        return true;
    }
相關文章
相關標籤/搜索