Recommended Two Receiving Methods for aspx Background Passing Json to Foreground

  • 2021-07-26 07:24:17
  • OfStack

Type 1: Foreground reception


dataType: "json",
 success: function (data)
 {
	var varReceiver = data;
 }

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <title> </title>
  <!--  Introduce  ECharts  Documents  -->
  <script src="scripts/echarts.common.min.js"></script>
  <script src="scripts/jquery-1.10.2.min.js"></script>
  <script src="scripts/json2.js"></script>
</head>
<body>
<form id="form1" runat="server">

<!--  For  ECharts  Prepare 1 Having size (width and height) Dom -->
<div id="main" style="width:1000px;height:400px;">

</div>

<script type="text/javascript">

    var varAxis;
    var varSeries;
    //var varRtcNO = $("#txtHid").val();
    var varRtcNO = "35000002818";
    var jdata;

    var myChart = echarts.init(document.getElementById('main'));
    //  Display captions, legends, and empty axes 
    myChart.setOption({
      title: {
        text: ' Temperature graph '
      },
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          animation: false
        },
        formatter: function (params) {
          return params[0].name + '<br /> Temperature :' + params[0].value + ' ° C';
        }
      },
      legend: {
        data: [' Temperature ']
      },
      xAxis: {
        data: []
      },
      yAxis: {
        axisLabel: {
          formatter: '{value}  ° C'
        },
        min: 18,
        max:30
      },
      series: [{
        name: ' Temperature ',
        type: 'line',
        smooth: true,
        data: []
      }]
    });
    //  Load data asynchronously 
    $.ajax({
      type: "post",
      url: "getrtchistorydata.ashx?rtcno=" + varRtcNO,
      dataType: "json",
      success: function (data) {
        var varReceiver = data;
        //var varReceiver = jQuery.parseJSON(data);
        var varAxis=new Array() ;
        var varSeries = new Array(varReceiver.Count[0].total);
        
        for (var i = 0; i < varReceiver.Count[0].total; i++) {
          varAxis.push(varReceiver.Rows[i].RecordTime);
          varSeries[i] = varReceiver.Rows[i].RoomTemp;
        }
        //  Fill in data 
        myChart.setOption({
          xAxis: {
            data: varAxis
          },
          series: [{
            // Corresponding to the corresponding series according to the name 
            name: ' Temperature ',
            data: varSeries
          }]
        });
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
        alert(errorThrown);
      }
    });
    
  </script>
</form>
</body>
</html>

Type 2: Foreground reception


dataType: "text",
        success: function (data) {
          //var varReceiver = data;
          var varReceiver = jQuery.parseJSON(data);
 . . . . . 
}

The background 1 general handler ashx of both systems 1:


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

namespace RTC
{
  /// <summary>
  /// getrtchistorydata  Summary description of 
  /// </summary>
  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);// Remove the last 1 Comma 

      

      stbList = stbList + "],"; 
      stbList = stbList + "\"Count\":[{\"total\":" + ds.Tables[0].Rows .Count+ "}]";// Used to record 1 A total of several data records were returned 
      
      stbList = stbList + "}";

      context.Response.Write(stbList.ToString());
    }

    public bool IsReusable
    {
      get
      {
        return false;
      }
    }

    public void RetrunHistoryData()
    {


    }
  }
}

Related articles: