2017-01-09 70 views
1
返回

請參閱下面的代碼:見JSON通過AJAX

<script type="text/javascript" src="Javascript/json2.js"></script> 
    <script type="text/javascript" src="Javascript/jquery-1.11.1.min.js"></script> 
    <script type = "text/javascript"> 
     function GetData() { 
      $.ajax({ 
       type: "POST", 
       url: "JSONExample.aspx/GetPerson", 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: OnSuccess(), 
      //async: false, 
      failure: function (response) { 
       alert('there was an error counting possibles') 
      } 
     }); 

     function OnSuccess() { 
      return function (response) { 
       alert(response.d); 
      } 
     } 
     } 
     GetData() 
    </script> 

我相信response.d返回desterilized JSON。我如何看到JSON,以便我可以將它自己殺滅爲.NET對象?

+0

你會得到一個答案客戶端(Javascript)。你怎麼能使用一個.NET對象? – Goufalite

回答

0

試試這個:

<script type="text/javascript" src="Javascript/json2.js"></script> 
    <script type="text/javascript" src="Javascript/jquery-1.11.1.min.js"></script> 
    <script type = "text/javascript"> 
     function GetData() { 
      $.ajax({ 
       type: "POST", 
       url: "JSONExample.aspx/GetPerson", 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: OnSuccess, 
      //async: false, 
      failure: function (response) { 
       alert('there was an error counting possibles') 
      } 
     }); 

     function OnSuccess(data) { 
      alert(data); 
     } 
     GetData() 
    </script> 

此外,您還可以調試與您的Internet瀏覽器(默認爲F12)的developper工具的信息。

+0

謝謝。我在這裏問了另一個類似的問題,以防萬一你有機會:http://stackoverflow.com/questions/41549052/json-not-parsed-into-custom-object – w0051977

1

嘗試用你替換這個代碼,如果你已經印刷JSON格式的陣列上這個網址JSONExample.aspx/GetPerson

如果您還沒有印在JSON數組,然後下面的代碼將無法正常工作。

<script type = "text/javascript"> 
     function GetData() { 
      $.ajax({ 
       type: "POST", 
       url: "JSONExample.aspx/GetPerson", 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: OnSuccess(response), 
      //async: false, 
      failure: function (response) { 
       alert('there was an error counting possibles') 
      } 
     }); 

     function OnSuccess(response) { 
      return function (response) { 
      var data = JSON.parse(response); 
       console.log(data); 
      } 
     } 
     } 
     GetData() 
    </script> 

您將能夠在開發人員工具的控制檯中看到響應。

+0

謝謝。用於引用JSON.Convert的+1。 我在這裏問過另一個類似的問題,以防萬一你有機會:stackoverflow.com/questions/41549052/ – w0051977