Ajax的post请求属性contentType若不是“application/x-www-form-urlencode”,若是“application/json”时,HttpServletRequest的实现类不会解析表单数据来放在request parameter map中,所以通过request.getParameter("XX")怎么也接收不到数据。代码如下:
js代码:
function newFunc(){
var params = {};
params.aa = ‘XX’;
$.ajax({
async:false,
contentType:'application/json',
url:projectUrl+'chk/ajax/ChkMessage.xhtml',
type:'post',
data:params,
扫描二维码关注公众号,回复:
8122928 查看本文章
dataType:'json'
});
}
java代码:
@RequestMapping("/{channel/ChkMessage.xhtml}")
public void chkUserMessage(HttpServletRequest request,HttpSerlvetResponse response){
String aa = request.getParameter("aa");//此处的aa为null
}
解决方式:
①将ajax换成get请求;
②contentType不使用”application/json”.