日期:2014-05-16  浏览次数:20345 次

简单的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");
            });
        });        
    <