2012-05-06 39 views
0
$.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“date_notify”我有这样的错误:的JavaScript遗漏的类型错误:无法读取属性未定义

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部分我没有LIMIT的查询。这是我的查询

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 

有什么可能的方法来改变这个吗?

回答

1

使用AND在循环编条件:

for(var i=0; i<responseJSON.length && i<10; i++) 

这样的环切出来的时候i或者超过条目的数量或达到10,先发生者为准。如果条目数小于10,则第一个条件首先变为假,如果有超过10条记录,则第二个条件首先变为假。

+0

这太好了!感谢兄弟不知道这一点 –

1

限制您的SQL查询。没有理由为您不会使用的数据使用网络带宽和服务器资源,如果数据敏感,不要说潜在的安全问题。

相关问题