2016-04-21 66 views
0

響應我打電話從其他功能的Ajax功能,並希望得到的響應,然後解析Json,以便將其分配給新div秒。使用/循環從Ajax調用從另一個功能

電話可以撥出這樣的:thedata = GetChequesByBatchID(batchId);,這是響應:

enter image description here

於是,我試圖通過響應循環,但是這是問題的所在。我不知道如何獲得響應並通過thedata進行循環。該數據應分配給htmlFromJson,以便將其作爲divs的組插入TR中。有任何想法嗎?

我的功能:

<script type="text/javascript"> 
    $(document).ready(function (params) { 
     var batchId; 
     var thedata; 
     var htmlFromJson = ""; 
     $('.showDetails').click(function() { 
      // Show Details DIV 
      $(this).closest('tr').find('.details').toggle('fast'); 
      batchId = $(this).data('batchid'); 
      thedata = GetChequesByBatchID(batchId); 

      var json = jQuery.parseJSON(thedata); 

      $.each(json, function() { 

        htmlFromJson = htmlFromJson + ('<div class="ListTaskName">' + this.ChequeID + '</div>' + 
         '<div class="ListTaskDescription">' + this.ChequeNumber + '</div>' + 
        '<div class="ListTaskDescription">' + this.ChequeAccountNumber + '</div>' + 
        '<div class="ListTaskDescription">' + this.ChequeAmount + '</div>'); 

      }); 
     }).toggle(
      function() { 
       // Trigger text/html to toggle to when hiding. 
       $(this).html('Hide Details').stop(); 
       $(this).closest("tr").after("<tr class='456456'><td></td><td colspan = '999'>" + '<div class="zzss">' + htmlFromJson + '</div></td></tr>');    
      }, 
      function() { 
       // Trigger text/html to toggle to when showing. 
       $(this).html('Show Details').stop(); 
       //$(this).find('.zoom').remove(); 
       $('tr.456456').remove(); 
      } 
     ); 
    }); 
</script> 

我的Ajax功能:

<script type="text/javascript"> 
    function GetChequesByBatchID(BatchID) { 
     var xss; 
     var qstring = '?' + jQuery.param({ 'BatchID': BatchID }); 
     return $.ajax({ 
      url: '<%=ResolveUrl("~/DesktopModules/PsaMain/API/ModuleTask/GetChequesByBatchID")%>' + qstring, 
      type: "GET", 
      cache: false, 
      contentType: "application/json; charset=utf-8", 
      success: function (result) { 
       jQuery.parseJSON(result); //the response 
      }, 
      error: function (response) { 
       alert("1 " + response.responseText); 
      }, 
      failure: function (response) { 
       alert("2 " + response.responseText); 
      } 
     }); 
     return xss; 
    } 
</script> 

回答

0

$.ajax是異步操作(如果不包括,不建議async:false選項),所以你GetChequesByBatchID函數立即返回或者$.ajax對象或undefined$.ajax的正確用法是調用$.ajaxsuccesserror部分的DOM更改方法。