2013-11-22 3 views
0

저는 한 시간 쯤이나이 작업을 해왔지만 알아낼 수 없습니다.JQuery는 일부 JSON 데이터에 액세스합니다.

JSON 응답은 : this.lastId에 대한

function longPolling() { 
    if (!longPollingAllowed) { 
     return; 
    } 
    console.log("Long polling started."); 
    $.ajax({ 
     url: "logpersonal_ajax.php", 
     data: { 
      serverId: serverId, 
      playerName: playerName, 
      lastDate: lastDate, 
      lastId: lastId 
     }, 
     cache: false, 
     dataType: "json", 
     beforeSend: function() { 
      longPollingBusy = true; 
     }, 
     success: function(json) { 
      console.log("success"); 
      $(json).each(function() { 
       console.log("this.lastDate = " + this.lastDate); 
       console.log("this.lastId = " + this.lastId) 
       console.log("this.response = " + this.response); 
       console.log(this.response); 
       this.lastDate = this.lastDate; 
       this.lastId = this.lastId; 
       if (this.response != "") { 
        this.response.each(new function() { 
         console.log(this); 
         var clazz = this.class; 
         console.log("clazz = " + clazz); 
         var id = this.id; 
         var date = this.date; 
         var player = this.player; 
         var target = this.target; 
         var weapon = this.weapon; 
         var server = this.server; 
         var string = "\t\t\t<tr class='" + clazz + "' id='" + id + "'><td>" + date + "</td><td>" + player + "</td><td>" + target + "</td><td>" + weapon + "</td><td>" + server + "</td></tr>\n"; 
         console.log("string = " + string); 
         $(string).insertBefore($("#list tr.header").next());    
        }); 
       } 
      }); 
      if (lastDate != "" && lastId != "") { 
       //longPolling(serverId, playerName, lastDate); 
      } 
      longPollingBusy = false; 
     }, 
     error: function(json, message) { 
      console.log("fail: " + message); 
      longPollingBusy = false; 
     } 
    });  
} 

console.log("this.lastDate = " + this.lastDate); 작품, 그렇게 하나 :

{"lastDate":"2013-11-22 00:00:35", 
"lastId":"42460", 
"response":[ 
{ 
"class":"rowgreen", 
"id":"42460","date":"22 November 2013, 00:00:35\\u0026nbsp;", 
"player":"\\u003Ca href=\\u0027logpersonal.php?playerName=skiwi2\\u0027\\u003Eskiwi2\\u003C\/a\\u003E\\u0026nbsp;", 
"target":"\\u003Ca href=\\u0027logpersonal.php?playerName=UnholiestElite\\u0027\\u003EUnholiestElite\\u003C\/a\\u003E\\u0026nbsp;", 
"weapon":"M1014 (\\u003Cb\\u003EHeadshot\\u003C\/b\\u003E)\\u0026nbsp;", 
"server":"Test\\u0026nbsp;" 
} 
]} 

이 올바른, 이제 JQuery와 것 같다. this.response도 잘 작동하여 인덱스 0으로 시작하는 배열을 보여 주며 확장하면 개발자보기의 모든 요소를 ​​볼 수 있습니다.

지금은 이해할 수없는 것 부분 온다 다음 foreachthis.response 이상이 this에 대한 (프로토 타입 골격 제외) 유용한 아무것도 인쇄되지 않습니다에서.

어떻게 값에 액세스 할 수 있습니까?

+1

* "foreach"*라고 말하면이 라인을 참조하고 있습니까? 'this.response.each (new function() {'그것은 forEach 일 것입니다.) 일단 forEach로 변경되면, 배열 값에 접근하기 위해'this'보다는 콜백의 매개 변수를 사용할 필요가 있습니다 –

+0

'this.response.each'는 배열에'each' 메쏘드가 없기 때문에 작동하지 않아야합니다 .. 또한'new function() {...}'은 틀리면 * 인스턴스 * (즉, 객체를)'.each '로 변환 할 것을 권장합니다. jQuery 메서드를 사용하거나 네이티브 JS 메서드를 사용하는 것이 좋습니다. –

+0

왜 iterator를'$ (json) .each (function() {'? 루트 요소가 배열이 아닙니다. 반환 된 객체의 속성을 반복적으로 처리하려고합니까? 'this.lastId'와 같은 것을 사용하려고 시도하는 것처럼 보이지 않습니다. –

답변

3
this.response.each(new function() { 

이 줄이 잘못되었습니다. 다음과 같아야합니다 :

this.response.forEach(function() { 

P. $(json).each(function() { 대신 $.each(json, function(){을 제안합니다.

관련 문제