2012-05-06 96 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查詢。沒有理由爲您不會使用的數據使用網絡帶寬和服務器資源,如果數據敏感,不要說潛在的安全問題。

相關問題