作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
$.ajax({
url: "notifications.php",
dataType: "json",
success: function(responseJSON) {
if (responseJSON.length > 0) {
document.title = document.title.replace(/^(?:\(\d+\))?/, "(" + responseJSON.length + ") ")
for (var i=0; i<10; i++) {
console.log(responseJSON[i].date_notify')
}
}
}, error : function(x) {
console.log(x.responseText)
}
})
在 Chrome 中我遇到此错误:
Uncaught TypeError: Cannot read property 'date_notify' of undefined
我在这部分发现了for (var i=0; i<10; i++)
应替换为 for (var i=0; i<responseJSON.length; i++)
问题是我只想得到 10 个结果...在我的 sql 部分我没有限制查询。这是我的查询
SELECT users.lastname, users.firstname, users.screenname, notifications.notify_id,
notifications.tagged_by, notifications.user_id, notifications.post_id,
notifications.type, notifications.action, notifications.date_notify,
notifications.notify_id
FROM website.users users INNER JOIN website.notifications notifications
ON (users.user_id = notifications.user_id)
WHERE notifications.user_id = ? and notifications.action = ?
ORDER BY notifications.notify_id DESC
//LIMIT 10
有什么办法可以改变这个吗?
最佳答案
在循环中使用 AND
ed 条件:
for(var i=0; i<responseJSON.length && i<10; i++)
这样,当 i
超过条目数或达到 10(以先发生者为准)时,循环就会中断。如果条目数小于10,则第一个条件先为假,如果记录数超过10,则第二个条件先为假。
关于javascript 未捕获类型错误 : Cannot read property 'date_notify' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10471361/
$.ajax({ url: "notifications.php", dataType: "json", success: function(respo
我是一名优秀的程序员,十分优秀!