程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> aspx後台傳遞Json到前台的兩種接收方法推薦

aspx後台傳遞Json到前台的兩種接收方法推薦

編輯:ASP.NET基礎

第一種:前台接收

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>
  <!-- 引入 ECharts 文件 -->
  <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">

<!-- 為 ECharts 准備一個具備大小(寬高)的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'));
    // 顯示標題,圖例和空的坐標軸
    myChart.setOption({
      title: {
        text: '溫度曲線圖'
      },
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          animation: false
        },
        formatter: function (params) {
          return params[0].name + '<br />溫度:' + params[0].value + '°C';
        }
      },
      legend: {
        data: ['溫度']
      },
      xAxis: {
        data: []
      },
      yAxis: {
        axisLabel: {
          formatter: '{value} °C'
        },
        min: 18,
        max:30
      },
      series: [{
        name: '溫度',
        type: 'line',
        smooth: true,
        data: []
      }]
    });
    // 異步加載數據
    $.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;
        }
        // 填入數據
        myChart.setOption({
          xAxis: {
            data: varAxis
          },
          series: [{
            //根據名字對應到相應的系列
            name: '溫度',
            data: varSeries
          }]
        });
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
        alert(errorThrown);
      }
    });
    
  </script>
</form>
</body>
</html>

第二種:前台接收

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

兩者統一的後台 一般處理程序ashx:

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

namespace RTC
{
  /// <summary>
  /// getrtchistorydata 的摘要說明
  /// </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);//去掉最後的一個逗號

      

      stbList = stbList + "],"; 
      stbList = stbList + "\"Count\":[{\"total\":" + ds.Tables[0].Rows .Count+ "}]";//用來記錄一共返回了幾條數據記錄
      
      stbList = stbList + "}";

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

    public bool IsReusable
    {
      get
      {
        return false;
      }
    }

    public void RetrunHistoryData()
    {


    }
  }
}

以上這篇aspx後台傳遞Json到前台的兩種接收方法推薦就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved