• 简单的ajax获取json


    一个DBhelper类,用来操作数据库

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;

    namespace JsonTest.helper
    {
        public class DBhelper
        {
            public static string ConnectStrings;

            public static void GetConnectStr()
            {
                ConnectStrings = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStr"].ConnectionString;
            }
            public static SqlDataReader GetCommentByID(int id,string sql)
            {
                GetConnectStr();
                SqlDataReader rs = null;
                try
                {
                    SqlConnection con = new SqlConnection(ConnectStrings);
                    con.Open();
                    SqlCommand com = new SqlCommand(sql, con);
                    rs=com.ExecuteReader();
                }
                catch (Exception err)
                {
                    throw err;
                }
                return rs;
            }
        }
    };

    前台htm页发出ajax post请求

    <!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></title>
        <script type="text/javascript" src="Scripts/jquery-1.4.1.js"></script>
        <script type="text/javascript">
            $(document).ready(function () {
                $("#button1").click(function sys() {
                    $.post("html_1.ashx", { "ID": 1 }, function sys(json) {
                        var str = json.item1 + json.item2 + json.item3 + json.item4;
                        $("ul").append("<li>" + str + "</li>");
                    }, "json");
                });
            });        
        </script>
    </head>
    <body>
        <input type="button" id="button1" value="点击" />
        <ul>
        </ul>
    </body>
    </html>

    后台ashx处理程序处理请求

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Data;
    using System.Data.SqlClient;
    using JsonTest.helper;
    using System.Web.Script.Serialization;

    namespace JsonTest
    {  
        /// <summary>
        /// html_1 的摘要说明
        /// </summary>
        public class html_1 : IHttpHandler
        {

            public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "text/plain";
                int id = Convert.ToInt32(context.Request["ID"]);
                string sql = "select * from item where ItemID=" + id + "";
                SqlDataReader sdr = null;
                sdr=DBhelper.GetCommentByID(id, sql);
                item Item = new item();
                while (sdr.Read())
                {

                    Item.item1 = sdr[0].ToString();
                    Item.item2 = sdr[1].ToString();
                    Item.item3 = sdr[2].ToString();
                    Item.item4 = sdr[3].ToString();
                }
                sdr.Close();
                sdr = null;
                JavaScriptSerializer js = new JavaScriptSerializer();
                string json = js.Serialize(Item).ToString();
                context.Response.Write(json);
            }

            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }

            public class item
            {
                public string item1;
                public string item2;
                public string item3;
                public string item4;
            }
        }
    }

    这样就可以无刷新的实现

  • 相关阅读:
    find指令使用手册
    IP封包协议头/TCP协议头/TCP3次握手/TCP4次挥手/UDP协议头/ICMP协议头/HTTP协议(请求报文和响应报文)/IP地址/子网掩码(划分子网)/路由概念/MAC封包格式
    Vmare虚拟机中的3种网络连接方式
    Windows10下Apache2.4配置Django
    网站配色
    js 图片轮播
    Window10下Apache2.4的安装和运行
    sqlite数据库转换为mysql数据库
    windows10 安装 mysql 5.6 教程
    win10 nginx + django +flup 配置
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3303930.html
Copyright © 2020-2023  润新知