2012-04-01 14 views
1

を示す表を構築するためにJSONデータを取得するためのjQueryを使用して、私はこの未定義

{ 
secInfo: [ 
{ 
    loginResult: 1, 
    userId: "admin", 
    userName: "admin", 
    userpassword: "ramshyam" 
}, 
{ 
    address: "", 
    loginResult: 1, 
    userEmail: "", 
    userId: "anuraag", 
    userName: "Anuraag", 
    userpassword: "kk0903" 
} 
] 
} 

私はテーブル内のデータを表示するためにjqueryのコードの下に使用する場合、それは "と表に移入ように見えるJSONを持っています

<script> 
$(document).ready(function(){ 
    //attach a jQuery live event to the button 
    $('#submitlogin').live('click', function(){ 
     $.getJSON('/acc/services/json/security/loginresult.json', function(data) { 
      //alert(data); //uncomment this for debug 
      //alert (data.item1+" "+data.item2+" "+data.item3); //further debug 
      // $('#showdata').html("<p>Result="+data.loginResult+" userid="+data.userId+" username="+data.userName+"</p>"); 
      $.each(data.secInfo, function(){ 

       $('#usertable').append(
          function(this){ 
           return "<tr>"+ 
              "<td>"+this.userId+"</td>"+ 
              "<td>"+this.userName+"</td>"+ 
              "<td>"+this.userpassword+"</td>"+ 
              "<td>1</td>"+ 
             "<tr>"; 
          } 
          ); 

       }) 
     }); 
    }); 
}); 
</script> 

」未定義の私はjQueryのに新しいですし、http://forum.jquery.com/topic/jquery-create-table-from-json-dataこの記事からいくつかのアイデアを得ました。

問題は、「これは」あなたの関数コールバックでは、予約されたキーワードをthatsのかもしれません

答えて

0

これは間違いなく、それはまだあなたのコールバックの内側にはconsole.log(これを)しようと、作業、または別の変数名に変更していない場合

<script> 
$(document).ready(function(){ 
    //attach a jQuery live event to the button 
    $('#submitlogin').live('click', function(){ 
     $.getJSON('/acc/services/json/security/loginresult.json', function(data) { 

    var genericlist = eval('(' + data+ ')'); 

var table = document.getElementById("usertable"); 
var tabledata = ""; 

for(i=0;i<genericlist.secInfo.length;i++){ 

tabledata += "<tr>"; 
tabledata += "<td>" + genericlist.secInfo[i].useID += "</td>"; 
tabledata += "<td>" + genericlist.secInfo[i].useName += "</td>"; 
tabledata += "<td>" + genericlist.secInfo[i].usePassword += "</td>"; 
tabledata += "</tr>"; 

} 

table.innerHTML = tabledata; 
0

私を助けてください。また、クラスプロパティの代わりにJSONエントリに配列としてアクセスしてみてください。

+0

を動作し、どのようなウェブ見ますインスペクタコンソールが出力しています。 –

+0

javascriptオブジェクトは設計上すべての連想配列であるため、この['userId']のようにアクセスする方が簡単かもしれません。また、「this」は異なるスコープを指します。 –

+0

'これはコンソール上でうまく書いてあり、useridで警告しています。関数内でオブジェクトを取得していない理由を知らない – antnewbee

0

「this」の範囲が変更されていると思います。これを試してみてください:

$(document).ready(function(){ 
    //attach a jQuery live event to the button 
    $('#submitlogin').live('click', function(){ 
     $.getJSON('/acc/services/json/security/loginresult.json', function(data) { 
      //alert(data); //uncomment this for debug 
      //alert (data.item1+" "+data.item2+" "+data.item3); //further debug 
      // $('#showdata').html("<p>Result="+data.loginResult+" userid="+data.userId+" username="+data.userName+"</p>"); 
      $.each(data.secInfo, function(dataItem){ 
       $('#usertable').append(
        function() { 
         return "<tr>"+ 
            "<td>"+dataItem.userId+"</td>"+ 
            "<td>"+dataItem.userName+"</td>"+ 
            "<td>"+dataItem.userpassword+"</td>"+ 
            "<td>1</td>"+ 
           "<tr>"; 
        } 
       ); 
      }) 
     }); 
    }); 
}); 
+0

ありがとうMateusz ..しかし、これも動作しませんでした:9 – antnewbee