后台参数存在方才有其属性的存在……
文章目录
在开发过程中ajax请求对得到的额数据进行操作 如下:
var strTimeStr=data.startTime;
var newstartTime=startTime=new Date(Date.parse(strTimeStr.replace(/-/g,"/"))).getTime();
……
再自测时发现报错,以至于后面的代码都停止执行。
报错:js提示 Cannot read property 'replace' of undefined
原因是在某些情况下返回的data中没有参数“startTime” 故“replace”不存在。
改进:
if(data.startTime){ //当data.startTime存在时
var strTimeStr=data.startTime; //后台返回的额时间是“2016-12-12 00:00:00”格式的字符串
var newstartTime=startTime=new Date(Date.parse(strTimeStr.replace(/-/g,"/"))).getTime(); //后台返回的string时间转为时间戳
}else{
console.log("startTime:不存在");
};
要用这种转换 var newstartTime=startTime=new Date(Date.parse(strTimeStr.replace(/-/g,"/"))).getTime(); 时间格式为“2016-12-12 00:00:00”
IOS解析Date.parse("Mon Dec 12 2016 10:00:00").getTime()这样的时间格式时 ios报NaN;
所以后台返回时间格式尽量为时间格式为“2016-12-12 00:00:00”
如若笔记有误,望指出,非常感谢~

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论