ajax发送请求,后台接收不到的问题

前台代码:
$.ajax({
				type : 'POST',
				url : '<%=basePath %>user/getOrgRoles.do',
				data : {'orgId' : orgId},
				async: false,
				contentType: "application/json; charset=utf-8",  
				dataType : 'json',
				success : function(data){
					
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
				}
			}); 



后台代码:

@ResponseBody
	@RequestMapping("/getOrgRoles") 
	public Object getOrgRoles(@RequestParam(value = "orgId", required = true) String orgId) {

	    List tempUserList = null;
	    JSONObject jsonObjectFromMap = null;
	    try
        {
	        tempUserList = tempUserService.getOrgRole(Long.parseLong(orgId));
	        jsonObjectFromMap = JSONObject.fromObject(tempUserList); 
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
	    
	    return jsonObjectFromMap;
	}



问题出在:@RequestParam 不能接收 contentType : "application/json; charset=utf-8" 的请求。

如果后台必须要用@RequestParam,

前台2个地方必须改:

1、contentType 改为 "application/x-www-form-urlencoded"

2、data: 改为{'orgId':orgId},



你可能感兴趣的:(java学习)