一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

echarts+ashx+json获取数据展示例子

时间:2016-06-01 编辑:简简单单 来源:一聚教程网

ashx是net提供的一般处理程序,比aspx返回的数据量少很多,适合做接口。

html代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="echart2.aspx.cs" Inherits="RTC.echart2" %>




   
   
   
   
   








js代码:


    url: "getrtchistorydata.ashx?rtcno=" + varRtcNO,就是服务端接收参数和返回数据的方法。

服务端方法:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
namespace RTC
{
    ///


    /// getrtchistorydata 的摘要说明
    ///

    public class getrtchistorydata : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string strRTCNo = context.Request.QueryString["rtcno"].ToString();
            SqlConnection con = new SqlConnection("server=192.168.0.222;uid=sa;pwd=hiwits;database=CeShi_QingDao;Max Pool Size=2048;");
            SqlCommand cmd = new SqlCommand("select RtcNO,RoomTemp,InstallPlace,convert(varchar,RecordTime,120) as RecordTime,systime  from RTCHistory where RtcNO='" + strRTCNo + "' order by InstallPlace,RecordTime", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            string stbList = "";
            stbList = "{\"Rows\":[";
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                stbList = stbList + "{ \"RecordTime\":\"" + dr[3].ToString() + "\",";
                stbList = stbList + " \"RoomTemp\":\"" + dr[1].ToString() + "\"},";
            }
            stbList = stbList.Substring(0, stbList.Length - 1);//去掉最后的一个逗号
            
            stbList = stbList + "],";
            stbList = stbList + "\"Count\":[{\"total\":" + ds.Tables[0].Rows .Count+ "}]";//用来记录一共返回了几条数据记录
           
            stbList = stbList + "}";
            context.htm = htm&(stbList.ToString());
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        public void RetrunHistoryData()
        {
        }
    }
}

其中,ajax的参数: dataType: "text",也可以是 dataType: "json",服务端在返回的时候,如果是json就序列化一下。请看参考。

在ajax成功后,反序列化一下就行。var varReceiver = jQuery.parseJSON(data);

热门栏目