网站首页  汉语字词  英语词汇  考试资料  写作素材  旧版资料

请输入您要查询的考试资料:

 

标题 jQuery+ajax+asp.net获取Json值的方法
内容
    本文实例讲述了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 Text
    function 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;
      }
    }
    希望本文所述对大家jQuery程序设计有所帮助。
随便看

 

在线学习网考试资料包含高考、自考、专升本考试、人事考试、公务员考试、大学生村官考试、特岗教师招聘考试、事业单位招聘考试、企业人才招聘、银行招聘、教师招聘、农村信用社招聘、各类资格证书考试等各类考试资料。

 

Copyright © 2002-2024 cuapp.net All Rights Reserved
更新时间:2025/5/19 17:58:34