记录糟心的一次调试
程序员文章站
2022-06-03 23:21:41
...
今天在编写SSM项目时,我希望从shopmanagement.html跳转到shopoperation.html时实现http://localhost:8080/o2o/admin/shopmanagement?shopId=1href变为http://localhost:8080/o2o/admin/shopoperation?shopId=1。
首先贴出代码:
$(function() {
var shopId = getQueryString("shopId");
var shopInfoUrl = "/o2o/admin/getshopmanagementinfo?shopId=" + shopId;
$.getJSON(shopInfoUrl, function(data) {
if (data.redirect) {
window.location.href = data.url;
} else {
if (data.shopId != undefined &&data.shopId != null) {
shopId = data.shopId;
}
$("#shopInfo").attr("href", "/o2o/admin/shopoperation?shopId=" + shopId);
}
});
});
- data is not defined
将断点设置在第三行,执行到此处时,Fn+F6,当前执行行跳到最后一行。发现错误data is not defined。
此时我看警告提示:sm.min.js:10 Disable router function because of no .page elements。路由出错,经过仔细检查后路由正确,而且我也可以访问页面,这说明在后端路由设置没问题。
再想到data is not defined我想是否是getJSON获取失败,是不是modelMap返回失败。此时我将断点设置在后台。
@RequestMapping(value = "/getshopmanagementinfo", method = RequestMethod.GET)
@ResponseBody
private Map<String, Object> getShopManagementInfo(HttpServletRequest request){
Map<String, Object> modelMap = new HashMap<>();
long shopId=HttpServletRequestUtil.getLong(request, "shopId");
if(shopId<=0){
Object currentShopObj=request.getSession().getAttribute("currentShop");
if(currentShopObj==null){
modelMap.put("redirect",true);
modelMap.put("url","/o2o/admin/shoplist");
}else{
Shop currentshop=(Shop)currentShopObj;
modelMap.put("redirect",false);
modelMap.put("shopId",currentshop.getShopId());
}
}else{
Shop currentShop=new Shop();
currentShop.setShopId(shopId);
request.getSession().setAttribute("currentShop", currentShop);
modelMap.put("redirect",false);
}
return modelMap;
}
经过每一步的调试,发现每一个对象都被成功赋值,这表明modelMap返回成功。
此时我陷入了焦虑之中。百般尝试后,我看到前端调试提示下条信息
- Return Value:undefined
return value没有被定义,由于modelMap返回成功,则data中的数据没问题,则js中只剩下shopInfo可能存在问题,经过思考原来是html文件中没有加入id,给div块加入id=“shopInfo”,至此问题解决!问题是小问题,过程实属不易啊! - 总结
此次经验我发现,当错误发生时,首先要做的应该是理解报错信息,然后思考报错可能是由何导致的,盲目的上网查错有可能会花费很多不必要的时间。这真的很关键!!!
上一篇: python获取网卡的ip子网掩码和网关