我有一個codeigniter應用程序,在我的一個視圖中,我有一個ajax調用返回json數據的API。一旦我得到數據,我通過它循環並將記錄追加到現有的表。 有兩種方法可以運行此ajax調用。一種是請求「全部」數據,另一種是按位置過濾。 這兩種方式都會返回數據,但是當Im試圖循環過濾數據集時,ajax失敗。迭代和顯示json數據
下面是通過記錄集循環代碼:
if (JSONdata.length != 0)
{
//create a heading row and attach it to the existing table.
var heading = $('<tr id="tblheading" naming="tblheading">').appendTo($('#switchrecords'));
heading.append($('<td>').append().text('Name'));
heading.append($('<td>').append().text('Object ID'));
//loop through each JSONdata item in the array and append another row to the switchrecords table.
$.each(JSONdata, function(i, objswitch) {
console.log('objectid is:'.objswitch.id);
console.log(objswitch.id);
var row = $('<tr>').appendTo($('#switchrecords'));
row.append($('<td>').append($('<a href='+ BASEPATH + 'index.php/controller/methodname/' + objswitch.id + '>').text(objswitch.name)));
row.append($('<td>').append(objswitch.id).text(objswitch.id));
});
這是我到目前爲止已經完成:
我確信,這兩個結果集具有相同的字段,即「id」和「name」。請注意,過濾的數據集包含比未過濾的結果更多的字段,但我認爲這不重要。
我已經使用console.log轉儲兩個結果集...這裏是兩個片段。第一個是ALL,另一個是過濾。
[{「name」:「888-12-993-99-1」,「id」:「1」,「dict_value」:「compact」},{「name」:「888-22-SR1 -RTR-1" , 「ID」: 「2」, 「dict_value」: 「緊湊」},{ 「名稱」: 「888-21-SR1-SW-1」, 「ID」: 「3」,「dict_value 「:」compact「},{」name「:」888-11-SR2-SW-2「,」id「:」4「,」dict_value「:」compact「},....等等
[{ 「PARENT_ID」: 「2」, 「TAG_ID」: 「10」, 「位置」: 「號樓」, 「ID」: 「7」, 「名稱」: 「888-22-228-22-1」, 「標籤」:NULL, 「asset_no」: 「1026067」, 「objtype_id」: 「1503」},{ 「PARENT_ID」: 「2」, 「TAG_ID」: 「5」, 「位置」: 「Building2」,「ID 「:」6「,」名稱「:」888-2-263-88-1「,」標籤「:空,」asset_no「:」1026068「,」objtype_id「:」1503「},....等
從代碼片段中可以看到,我試圖添加一些調試信息看看循環中發生了什麼。但是,我發現了以下錯誤消息在所述第一的console.log()調用:
[17:13:30.675]類型錯誤: 「的objectid是:」 objswitch未定義
予。我不太清楚如何解決這個問題。任何建議,將不勝感激。 如果這是一個愚蠢的錯誤,我很抱歉!我一整天都在編程,我的腦子很好! =)
在此先感謝您的幫助。
不知道怎麼的console.log'( 'OBJECTID是:' this.id);'沒有錯誤將執行。 '''應該導致它在你的具體情況下拋出錯誤:'未捕獲的SyntaxError:意外的字符串' – Nope
@FrançoisWahl.. oops錯過了..謝謝指出它..我忽略了那裏的語法 –
@sushanth!謝啦!就是這樣。我可以繼續前進並繼續排查問題! – dot