后台处理: @RequestMapping("/querySectors")
public ModelAndView querySectors()throws Exception{
//调用service查找数据库
List<Sectors> sectorsList = sectorsService.findSectorsByName(null);
//返回modelandview
ModelAndView modelAndView = new ModelAndView();
/*modelAndView.addObject("sectorsList", sectorsList);
System.out.println(sectorsList);*/
JSONArray jsay=JSONArray.fromObject(sectorsList);
System.out.println(jsay);
modelAndView.setViewName("sectors/sectorsList");
return modelAndView;
前台控制器:
var url = "http://localhost:8080/jjzdoa/querySectors.action";
$http(
{
method : 'POST',
url : url,
datatype:'json',
params :{userid:'',password:''}, // 传递数据作为字符串,从前台传到后台
}
).success(function (data) {
confirm(data);
/* $scope.msg = data.msg ;
if(data.msg==true){
localStorage.setItem("userId", name);
window.location = "../kuaishuxd.html";
} else{
confirm("用户名或密码错误!");
} */
});
}
});
打印的代码在哪呢???
考虑 ajax是异步的, 打印的js也要在异步触发之后触发
你的window.location = "../kuaishuxd.html"; 代码不就是打开新的页面吗,所以你看到新的页面。
你说的打印数据,是控制台打印,还是刷新页面啊?
后端不是返回 ModelAndView ,应该返回json格式的数据 或者 ResponseEntity
比如:
@RequestMapping("/completed/{entity}/{client_id}.json")
public ResponseEntity individual(@PathVariable("entity") String clientEntity, @PathVariable("client_id") int clientId) {
ApplySearchVO search = new ApplySearchVO();
search.setClientEntity(clientEntity);
search.setClientId(clientId);
search.setStatus(ApplyStatus.COMPLETE.code);
List<ApplySearchVO> list = service.list(search);
List<Map<String, Object>> results = Lists.newArrayList();
for (Apply a : list) {
Map<String, Object> result = Maps.newHashMap();
result.put("id", a.getId());
result.put("pid", a.getProductId());
result.put("pname", a.getProductName());
result.put("code", a.getCode());
result.put("amount", a.getApplyLimit());
result.put("approvedAmount", a.getApprovedLimit());
results.add(result);
}
return new ResponseEntity(results, HttpStatus.OK);
}
@RequestMapping(value = "/checkEnterpriseCode.json")
@ResponseBody
public String checkEnterpriseCode(HttpServletResponse response, String code, Integer id) {
JsonObject json = new JsonObject();
Enterprise enterprise = service.selectByCode(code);
if (enterprise == null) {
json.addProperty("success", true);
}
else {
if(id == null) {// 新添加
json.addProperty("success", false);
}else {// 修改
if(id.intValue() == enterprise.getId().intValue()) {//code没有变动
json.addProperty("success", true);
}else {
json.addProperty("success", false);
}
}
}
return json.toString();
}
我也遇到过这样子的问题。