通过ajax提交数据SpringMVC后台按单个参数接受的三种方式

第一种:data请求参数是json格式,type为get方式,后台用@RequestParam接收
前端代码:

        var url = "/api/pmpro-base/attachmentUpload/getList";
        var data = {"jobTypeCode" : jobTypeCode,
                     "objectId" : objectId};
        $.ajax({
            type: 'GET',
            async: 'false',
            url: url,
            dataType: 'json',
            contentType: 'application/json',
            data: data,
            success: function (result) {
                if (result.resultState = 1) {
                } else {
                }
            }
        });

后端代码:

@RequestMapping("/getList")
	@ResponseBody
	public ResultVO getList(@RequestParam String jobTypeCode, @RequestParam String objectId) {
		  ...//代码
}

第二种:data请求参数是json格式,type为post方式,contentType不设置为默认的application/x-www-form-urlencoded,后台用@RequestParam接收
前端代码:

        var url = "/api/pmpro-base/attachmentUpload/getList";
        var data = {"jobTypeCode" : jobTypeCode,
                     "objectId" : objectId};
        $.ajax({
            type: 'POST',
            async: 'false',
            url: url,
            dataType: 'json',
            //contentType: 'application/json',
            data: data,
            success: function (result) {
                if (result.resultState = 1) {
                } else {
                }
            }
        });

后端代码:

@RequestMapping("/getList")
	@ResponseBody
	public ResultVO getList(@RequestParam String jobTypeCode, @RequestParam String objectId) {
		  ...//代码
}

第三种方式:请求参数写在请求路径上,type为get方式,@RequestMapping("/deleteAttachment/{jobTypeCode}/{objectId}/{attachmentIds}"),参数用@PathVariable接收
前端代码:

var url = "/api/pmpro-base/attachmentUpload/deleteAttachment"+"/"+jobTypeCode+"/"+objectId+"/"+attachmentIds;
console.log("attachmentIds------"+attachmentIds);
$.ajax({
    type: 'GET',
    async: 'false',
    url: url,
    dataType: 'json',
    contentType: 'application/json',
    success: function (result) {
        if (result.resultState = 1) {
        } else {
        }
    }
})

后端代码:

@RequestMapping("/deleteAttachment/{jobTypeCode}/{objectId}/{attachmentIds}")
	@ResponseBody
	public ResultVO deleteAttachment(@PathVariable String jobTypeCode, @PathVariable String objectId,
			@PathVariable String attachmentIds, HttpServletRequest request, HttpServletResponse response) {
		...//代码
}

你可能感兴趣的:(前后端交互)