问题描述
今天在写一个页面调用后端方法判断该用户是否为重要监控账户信息时,页面发出的AJAX请求访问后台方法时不能获取用户信息。
事例代码
function open_Stock(martketCode, openbizUnitid, bizId) {
$.ajax({
url : "ajaxStockAccount!isKeyAccountManagement.action?rnd=" + new Date().getTime(),
type : "POST",
success : function (data) {
var errorNo = data.resMap.errorNo;
if (errorNo == "0") {
var errorInfo = data.resMap.errorInfo;
showDialog(errorInfo);
return false;
} else {
canOpenStock(martketCode, openbizUnitid, bizId);
}
},
error : function () {
showDialog("系统超时,请重试");
}
});
}
/**
* 判断是否为重点监控账户
* @return
*/
public String isKeyAccountManagement() {
resMap = new HashMap<String, Object>();
try {
ObsUserProfile userProfile = super.getObsUserProfile();
if (userProfile != null) {
resMap = this.obsStockAccountService.isKeyAccountManagement(userProfile);
}
} catch (Exception e) {
log.info("isKeyAccountManagement判断重点监控账户异常");
e.printStackTrace();
}
return SUCCESS;
}
解决过程
- 首先想到的时AJAX方法写的有问题,经过排查后排除这种可能;
- 后台代码使用debug放式逐一排查,发现其他AJAX调用后台方法后request域中有用户信息,而自己写的AJAX请求后台方法request没有用户信息,不知怎么回事,于是查看日志;
- 通过查看日志发现自己写的AJAX调用后台方法时没有走拦截器,突然知道了可能的问题,因为拦截器方法中会获取用户信息,然后将用户存入request域中。
- 通过查看项目配置文件解决了这个问题,只要在filterUrls.xml文件中添加配置:
即可解决问题。<url value="/ajaxStockAccount!isKeyAccountManagement.action" type="3"/>