Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 10th, 2012  |  syntax: None  |  size: 1.64 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to send Json Data from Aspx page
  2. <script type="text/javascript" >
  3.     $(document).ready(function() {
  4.  
  5.     $("#txtTest").tokenInput("Complete.aspx", {
  6.         theme: "facebook"
  7.     });
  8.  
  9.     });
  10.  
  11.  
  12. </script>
  13.        
  14. protected void Page_Load(object sender, EventArgs e)
  15. {
  16.     if (!string.IsNullOrEmpty(Request.QueryString["q"]))
  17.     {
  18.         string json = "[{"Id":"1","name": "Test 1"},{"Id":"2","name": "Test 2"}]";
  19.         Response.Clear();
  20.         Response.ContentType = "application/json; charset=utf-8";
  21.         Response.Write(json);
  22.         Response.End();              
  23.  
  24.     }
  25. }
  26.        
  27. [WebMethod]
  28.     public static string Info()
  29.     {
  30.         JavaScriptSerializer js = new JavaScriptSerializer();
  31.         string result = js.Serialize(new string[] { "one", "two", "three" });
  32.         return result;
  33.     }
  34.        
  35. <script type="text/javascript">
  36.         $(function () {
  37.             $("#button1").click(function () {
  38.                 $.ajax({
  39.                     url: "Default.aspx/Info",
  40.                     data: "{}",
  41.                     contentType: "application/json",
  42.                     success: function (data) {
  43.                         alert(data.d);
  44.                     },
  45.                     type: "post",
  46.                     dataType : "json"
  47.                 });
  48.             });
  49.         });
  50. </script>
  51.        
  52. string json = "[{"name":"Pratik"},{"name": "Parth"}]";
  53.   Response.Clear();
  54.   Response.ContentType = "application/json; charset=utf-8";
  55.   Response.Write(json);
  56.   Response.End();
  57.        
  58. <script type="text/javascript">
  59.         $(function () {
  60.             $("#txt1").tokenInput("JsonPage.aspx");
  61.         });
  62. </script>
  63.  
  64. <body>
  65.  <input type="text" id="txt1"/>
  66. </body>