2013-06-12 2 views
1
function getReportGroups() { 
    $.ajax({ 
     contentType: "application/json; charset=utf-8", 
     url: "ReportGroups.ashx", 
     data: { 
      'method': 'getReportGroups', 
      'projectId': '30390' 
     }, 
     dataType: "json", 
     success: function (data) { 
      alert('inside success'); 
      var i = 0; 
      groupName = [data[i].name]; 
      while (data[i] != null) { 
       alert([data[i].name]); 
       alert([data[i].reportGroupId]); 
       $("#top-node").append("<li item-checked='true' item-expanded='true'><a href=# style=font-weight:bold>" + [data[i].name] + "</a>"); 
       i++; 
       var id = [data[i].reportGroupId]; 
       getReports(id); 
      } 

     }, 
     error: function (result) { 
      alert("Error- Inside the error loop"); 
     } 

    }); 
} 

function getReports(id) { 
    $.ajax({ 
     contentType: "application/json; charset=utf-8", 
     url: "ReportGroups.ashx", 
     data: { 
      'method': 'getReports', 
      'reportGroupId': id 
     }, 
     dataType: "json", 
     success: function (data) { 
      alert('inside getReports success'); 
      var i = 0; 
      groupName = [data[i].name]; 
      while (data[i] != null) { 
       alert([data[i].name]); 
       i++; 
      } 
     }, 
     error: function (result) { 
      alert("Error- Inside the error loop"); 
     } 
    }); 
} 

이것은 내 코드입니다. 여기서 getReports (id)를 getReportGroups()에서 매개 변수 id로 호출하면 getReoprts() 함수에서 id가 0으로 전달됩니다. 나는 그 문제가 뭔지 모르겠다. 나는 'id'가 첫 번째 것에 존재 하는지를 확인하기 위해 경고 박스를 사용했다. 나는 getReportsFunction에 유효한 ID를 가지고있다. 하지만 두 번째로 이드를 0으로 만듭니다. 여기서 내가 뭘 잘못하고 있니?jquery 함수를 호출하는 동안 값을 매개 변수로 전달하는 방법

+0

왜 배치되는 변수를 반복? 거기에서 문제를 추적하기 시작할 것입니다 ... – elclanrs

+0

첫 번째 메소드의 성공 호출에서 var id를 선언하고 있습니다. 어쩌면 아약스 호출 외부에서 선언 할 수 있습니다. 따라서 getReportGroups 바로 뒤에있는 아약스 호출 범위 외부에서 호출하십시오. 또한 getReports에있을 때 id가 정확하다는 것을 알리는 곳은 어디입니까? – Nicros

+0

@Nicros : 행 16 –

답변

0

var id = data[i].reportGroupId을 호출하기 전에 i이 증가하는 것처럼 보입니다. 여기서 첫 번째 반복 (alert([data[i].reportGroupId])의 값을 테스트합니다.).

i++을 while 루프의 마지막 문으로 이동하고 이것이 문제를 해결하는지 확인하십시오.

+0

문제는 동일하게 유지됩니다. –

0

문제점 i++ 같다하면 .each 사용할 수() 'groupName을 = 데이터 [I] .name을]'와 같은 배열의 데이터를 통해

$.each(data, function(idx, item){ 
    alert(item.name); 
    alert([item.reportGroupId]); 
    $("#top-node").append("<li item-checked='true' item-expanded='true'><a href=# style=font-weight:bold>" + [item.name] + "</a>"); 
    var id = [item.reportGroupId]; 
    getReports(id); 

}) 
관련 문제