jQuery+ajax+asp.net擷取Json值的方法_jquery

來源:互聯網
上載者:User

本文執行個體講述了jQuery+ajax+asp.net擷取Json值的方法。分享給大家供大家參考,具體如下:

<html xmlns="http://www.w3.org/1999/xhtml"><head>  <title>jQueryAjaxJson取值樣本</title>  <script type="text/javascript" src="Scripts/jquery-1.4.4.min.js"></script>  <script type="text/javascript">    $(document).ready(function () {      $("#Button1").click(function () {        $.ajax({          url: 'AjaxQuery.aspx',          type: 'GET',          dataType: 'json',          timeout: 1000,          cache: false,          beforeSend: LoadFunction, //載入執行方法          error: erryFunction, //錯誤執行方法          success: succFunction //成功執行方法        })        function LoadFunction() {          $("#ddd").html('載入中...');        }        function erryFunction() {          alert("error");        }        function succFunction(tt) {          $("#ddd").html('');          var json = eval(tt); //數組          $.each(json, function (index, item) {            //迴圈擷取資料            var name = json[index].Name;            var age = json[index].Age;            var sex = json[index].Sex;            $("#ddd").html($("#ddd").html() + "<br>" + name + " - " + age + " - " + sex + "<br/>");          });        }      });    })  </script></head><body>  <input type="button" id="Button1" value="擷取json資料" />  <span id="ddd"></span></body></html>
//Ajax Post Textfunction savedata(tempid) {  var tid = $('#hidtemplate').attr('value');  var desc = $("#contentdiv").html();  var num_iid = $("#num_iidArr").attr('value');  var num_iid2 = $("#num_iidArr001").attr('value'); //發佈頁面  var topsvalue = $("#tops").attr('value');  if (num_iid != "" && num_iid2 != "") {    $.ajax({      url: 'TabBaoHandler.ashx',      type: 'POST',      data: 'type=3&num_iid=' + num_iid2 + '&tid=' + tid + '&desc=' + desc + '&top_session=' + topsvalue,      dataType: 'text',      timeout: 20000,      cache: false,      //async: false, //同步      beforeSend: LoadFunction, //載入執行方法      error: erryFunction, //錯誤執行方法      success: succFunction //成功執行方法    })    function LoadFunction() {      showLoad("正在運行中...");    }    function erryFunction() {      $("#contentdiv").html("<p style=\"padding:5px\"><img src=\"images/error.png\" />sorry,提交失敗</p>");      closeLoad();    }    function succFunction(tt) {      closeLoad();      $("#contentdiv").show().html(tt);    }  } else {    alert("請選擇後再操作");  }}
using System;//新增using System.Web.Script.Serialization;using System.Collections.Generic;public partial class AjaxQuery : System.Web.UI.Page{  protected void Page_Load(object sender, EventArgs e)  {    if (!IsPostBack)    {      List<Student> list = new List<Student>();      Student c = new Student();      c.Name = "張三";      c.Age = 23;      c.Sex = "男";      list.Add(c);      Student cc = new Student();      cc.Name = "李四";      cc.Age = 25;      cc.Sex = "男";      list.Add(cc);      Student ccc = new Student();      ccc.Name = "李玲";      ccc.Age = 25;      ccc.Sex = "女";      list.Add(ccc);      Response.ContentType = "application/json";      Response.Write(new JavaScriptSerializer().Serialize(list));////這個很關鍵,否則error      Response.End();    }  }  public struct Student  {    public string Name;    public int Age;    public string Sex;  }}

PS:這裡推薦幾款本站的json格式資料線上操作工具供大家免費使用,相信在以後的開發中可以派上用場:

線上JSON代碼檢驗、檢驗、美化、格式化工具:
http://tools.jb51.net/code/json

線上XML/JSON互相轉換工具:
http://tools.jb51.net/code/xmljson

C語言風格/HTML/CSS/json代碼格式化美化工具:
http://tools.jb51.net/code/ccode_html_css_json

json代碼線上格式化/美化/壓縮/編輯/轉換工具:
http://tools.jb51.net/code/jsoncodeformat

更多關於jQuery相關內容感興趣的讀者可查看本站專題:《jquery中Ajax用法總結》、《jQuery表格(table)操作技巧匯總》、《jQuery拖拽特效與技巧總結》、《jQuery擴充技巧總結》、《jQuery常見經典特效匯總》、《jQuery動畫與特效用法總結》、《jquery選取器用法總結》及《jQuery常用外掛程式及用法總結》

希望本文所述對大家jQuery程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.