欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jQuery Ajax和getJSON獲取后臺普通json數(shù)據(jù)和層級json數(shù)據(jù)用法分析

 更新時間:2016年06月08日 09:19:46   作者:smartsmile2012  
這篇文章主要介紹了jQuery Ajax和getJSON獲取后臺普通json數(shù)據(jù)和層級json數(shù)據(jù)用法,結(jié)合實例形式分析了jQuery基于ajax操作json格式數(shù)據(jù)的相關(guān)技巧,需要的朋友可以參考下

本文實例講述了jQuery Ajax和getJSON獲取后臺普通json數(shù)據(jù)和層級json數(shù)據(jù)用法。分享給大家供大家參考,具體如下:

運行效果截圖如下:

具體代碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Ajax和getJSON獲取后臺普通Json數(shù)據(jù)和層級Json數(shù)據(jù)解析</title>
  <script src="JS/jquery-1.8.0.min.js" type="text/javascript"></script>
  <script type="text/javascript">
    $(function () {
      //方式一 Ajax方式獲取Json數(shù)據(jù)
      $.ajax({
        url: 'jsondata.ashx?type=1',
        type: 'GET',
        dataType: 'json',
        timeout: 1000,
        cache: false,
        beforeSend: LoadFunction, //加載執(zhí)行方法
        error: erryFunction, //錯誤執(zhí)行方法
        success: succFunction //成功執(zhí)行方法
      })
      function LoadFunction() {
        $("#list").html('加載中...');
      }
      function erryFunction() {
        alert("error");
      }
      function succFunction(tt) {
        var json = eval(tt); //數(shù)組
        var tt = "";
        $.each(json, function (index) {
          //循環(huán)獲取數(shù)據(jù)
          var Id = json[index].id;
          var Name = json[index].name;
          var Age = json[index].age;
          var Score = json[index].score;
          tt += Id + "___" + Name + "___" + Age + "___" + Score + "<br>";
        });
        $("#list").html('');
        $("#list").html(tt);
      }
      //方式二 Json方式獲取數(shù)據(jù)
      $.getJSON(
        "jsondata.ashx?type=1",
        function (data) {
          //循環(huán)獲取數(shù)據(jù)
          var tt = "";
          $.each(data, function (k, v) {
            $.each(v, function (kk, vv) {
              tt += kk + ":" + vv + "___";
            });
            tt += "<br/>";
          });
          $("#list2").html(tt);
        }
      );
      //方式三 Ajax方式獲取Json層級數(shù)據(jù)
      $.ajax({
        url: 'jsondata.ashx?type=3',
        type: 'GET',
        dataType: 'json',
        timeout: 1000,
        cache: false,
        beforeSend: LoadFunction1, //加載執(zhí)行方法
        error: erryFunction1, //錯誤執(zhí)行方法
        success: succFunction1 //成功執(zhí)行方法
      })
      function LoadFunction1() {
        $("#list3").html('加載中...');
      }
      function erryFunction1() {
        alert("error");
      }
      function succFunction1(tt) {
        var json = eval(tt); //數(shù)組
        var tt = "";
        $.each(json, function (index) {
          //循環(huán)獲取數(shù)據(jù)
          var Id = json[index].id;
          var Name = json[index].name;
          var Age = json[index].age;
          var Score = json[index].score;
          tt += Id + "___" + Name + "___" + Age + "___";
          $.each(Score, function (k, v) {
            tt += k + ":" + v + "___";
          })
          tt += "<br/>";
        });
        $("#list3").html('');
        $("#list3").html(tt);
      }
      //方式四 Json方式獲取層級數(shù)據(jù)
      $.getJSON(
        "jsondata.ashx?type=3",
        function (json) {
          //循環(huán)獲取數(shù)據(jù)
          var tt = "";
          $.each(json, function (index) {
            //循環(huán)獲取數(shù)據(jù)
            var Id = json[index].id;
            var Name = json[index].name;
            var Age = json[index].age;
            var Score = json[index].score;
            tt += Id + "___" + Name + "___" + Age + "___";
            $.each(Score, function (k, v) {
              tt += k + ":" + v + "___";
            })
            tt += "<br/>";
          });
          $("#list4").html('');
          $("#list4").html(tt);
        }
      );
    });
  </script>
</head>
<body>
  <p>方式一</p>
  <ul id="list">
  </ul>
  ____________________________________
  <p>方式二</p>
  <ul id="list2">
  </ul>
  ____________________________________
  <p>方式三</p>
  <ul id="list3">
  </ul>
  ____________________________________
  <p>方式四</p>
  <ul id="list4">
  </ul>
</body>
</html>

<%@ WebHandler Language="C#" Class="jsondata" %>
using System;
using System.Web;
using System.Web.Script.Serialization;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using Newtonsoft.Json;
public class jsondata : IHttpHandler {
  public void ProcessRequest(HttpContext context)
  {
    context.Response.ContentType = "text/plain";
    context.Response.Cache.SetNoStore();
    string type = context.Request["type"];
    if (type=="1") //普通數(shù)據(jù)
    {
      List<Dictionary<String, String>> aa = new List<Dictionary<string, string>>();
      for (int i = 0; i < 6; i++)
      {
        Dictionary<String, String> aaa = new Dictionary<string, string>();
        aaa.Add("id", "no" + i);
        aaa.Add("name", "張三" + i);
        aaa.Add("age", "21");
        aaa.Add("score", "1001");
        aa.Add(aaa);
      }
      string json = JsonConvert.SerializeObject(aa, Formatting.Indented);
      context.Response.Write(json);
    }
    if (type == "3") //層級數(shù)據(jù)
    {
      List<Student> list = new List<Student>();
      for (int i = 0; i < 6; i++)
      {
        Student a = new Student();
        a.id = "no" + i;
        a.name = "張三" + i;
        a.age = "21";
        Dictionary<string, string> dic = new Dictionary<string, string>();
        dic.Add("語文","80");
        dic.Add("數(shù)學(xué)", "81");
        dic.Add("英語", "83");
        dic.Add("生物", "89");
        dic.Add("化學(xué)", "90");
        dic.Add("物理", "95");
        a.score = dic;
        list.Add(a);
      }
      string json = JsonConvert.SerializeObject(list, Formatting.Indented);
      context.Response.Write(json);
    }
  }
  public struct Student
  {
    public string id;
    public string name;
    public string age;
    public Dictionary<string,string> score;
  }
  public bool IsReusable
  {
    get
    {
      return false;
    }
  }
}

更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jquery中Ajax用法總結(jié)》、《jQuery表格(table)操作技巧匯總》、《jQuery拖拽特效與技巧總結(jié)》、《jQuery擴展技巧總結(jié)》、《jQuery常見經(jīng)典特效匯總》、《jQuery動畫與特效用法總結(jié)》、《jquery選擇器用法總結(jié)》及《jQuery常用插件及用法總結(jié)

希望本文所述對大家jQuery程序設(shè)計有所幫助。

相關(guān)文章

  • jQuery的強大選擇器小結(jié)

    jQuery的強大選擇器小結(jié)

    jQuery的選擇器主要包括基本選擇器 子選擇器 特征選擇器 位置選擇器。
    2009-12-12
  • 比Jquery的document.ready更快的方法

    比Jquery的document.ready更快的方法

    這個是上次在博客園看到的一篇文章,經(jīng)測試,確實比jquery的$(document).ready(function(){....})更快,并且在ie和火狐等主流瀏覽器上都沒問題
    2010-04-04
  • jQuery下拉框的簡單應(yīng)用

    jQuery下拉框的簡單應(yīng)用

    這篇文章主要為大家詳細(xì)介紹了jQuery下拉框的簡單應(yīng)用,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-06-06
  • jQuery實現(xiàn)ctrl+enter(回車)提交表單

    jQuery實現(xiàn)ctrl+enter(回車)提交表單

    本文章來給大家介紹在我們輸入完內(nèi)容之后直接按Ctrl+Enter提交表單實現(xiàn)程序,此方法一般是用于textarea中哦,其它的input這類的就不需了。
    2015-10-10
  • jquery實現(xiàn)表單輸入時提示文字滑動向上效果

    jquery實現(xiàn)表單輸入時提示文字滑動向上效果

    這篇文章主要介紹了jquery實現(xiàn)表單輸入時提示文字滑動向上效果,涉及jquery鼠標(biāo)事件響應(yīng)及頁面元素樣式的動態(tài)操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-08-08
  • 寫JQuery插件的基本知識

    寫JQuery插件的基本知識

    這篇文章主要介紹了從如何寫JQuery插件,需要注意的事項,還有必須要做的步驟,看過這個文章相信你會明白如何寫好一個JQuery插件
    2013-11-11
  • JQuery使用數(shù)組遍歷跳出each循環(huán)

    JQuery使用數(shù)組遍歷跳出each循環(huán)

    這篇文章主要介紹了JQuery使用數(shù)組遍歷跳出each循環(huán),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-09-09
  • jQuery()方法的第二個參數(shù)詳解

    jQuery()方法的第二個參數(shù)詳解

    這篇文章主要介紹了jQuery()方法的第二個參數(shù)的使用方法,十分的簡單,有需要的小伙伴可以參考下。
    2015-04-04
  • jQuery中與toggleClass等價的程序段 以及未來學(xué)習(xí)的方向

    jQuery中與toggleClass等價的程序段 以及未來學(xué)習(xí)的方向

    昨天開始學(xué)jQuery,js是我前端設(shè)計技術(shù)的一塊心病,一直沒有找到很好的學(xué)習(xí)辦法。最近突然開悟,可以學(xué)jQuery呀,這個東西比較好學(xué)。
    2010-03-03
  • jQuery滾動加載圖片實現(xiàn)原理

    jQuery滾動加載圖片實現(xiàn)原理

    這篇文章主要介紹了jQuery滾動加載圖片實現(xiàn)原理,通過四個方面來說明懶加載技術(shù)的原理,感興趣的小伙伴們可以參考一下
    2015-12-12

最新評論