zoukankan      html  css  js  c++  java
  • 简单的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;
            }
        }
    }

    这样就可以无刷新的实现

  • 相关阅读:
    Django项目:CRM(客户关系管理系统)--54--45PerfectCRM实现账号快速重置密码
    Django项目:CRM(客户关系管理系统)--53--44PerfectCRM实现账号快速注册登陆
    Python 原生2种 邮件发送(发送验证码) 的方法
    Django项目:CRM(客户关系管理系统)--52--43PerfectCRM实现AJAX全局账号登陆
    Django项目:CRM(客户关系管理系统)--51--42PerfectCRM实现AJAX全局账号注册
    Django项目:CRM(客户关系管理系统)--50--41PerfectCRM实现全局账号密码修改
    Django项目:CRM(客户关系管理系统)--49--40PerfectCRM实现全局账号注册+验证码+页面刷新保留信息
    Django项目:CRM(客户关系管理系统)--48--39PerfectCRM实现登录+验证码+过期时间+页面保留账号
    mvc 伪静态任意扩展名的实现方法
    asp.net mvc各种传值方式大全
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3303930.html
Copyright © 2011-2022 走看看