2014-04-02 38 views
0

我想將數據庫查詢結果以JSON格式傳遞給jquery,爲此我想到了這一點 第一個函數將從某個c#函數獲取數據,然後將該數據作爲輸入傳遞給另一個jquery,以便在webservice上處理。調用一個函數,並將響應作爲來自另一個jQuery的JSON數據並獲得響應

我是新來的..有人可以幫我

第一功能:

<script type="text/javascript"> 

var Result1; 
var Result2; 

function btnJquery_Click() { 
    $.ajax({ type: "POST", 
     url: "Default.asmx/GetData", // this will get data from c# function 
     data: {}, 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function CallanotherFunction (response) { Result1= JSON.parse(response);} 
    }); 

function CallanotherFunction() { 
    $.ajax({ type: "POST", 
     url: "Default.asmx/GetData", // this will get data from c# function 
     data: Result1, 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function CallanotherFunction (response) {Result2= JSON.parse(response); } 
    }); 

    } 
</script> 

這只是虛構的代碼;將無法正常工作..請幫助我的代碼

回答

1

嘗試用.done()

function btnJquery_Click() { 
    $.ajax({ type: "POST", 
     url: "Default.asmx/GetData", // this will get data from c# function 
     data: {}, 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function (response) { 
     Result1= JSON.parse(response); 

     }).done(function(Result1){ 
     CallanotherFunction (Result1) 
     }) 
    }); 

OR

$.ajax({ 
    type: "POST", 
    url: "Default.asmx/GetData", // this will get data from c# function 
    data: {}, 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: function (data) { 
     Result1 = JSON.parse(data); //1st ajax data 
     $.ajax({ 
      type: "POST", 
      url: "Default.asmx/GetData", // this will get data from c# function 
      data: Result1, //used 1st ajax data 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: function (response) { 
       Result2 = JSON.parse(response);//here 2nd ajax data based on first ajax call 
      } 
     }); 
    } 
}); 
相關問題