服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - ASP.NET教程 - aspx后台传递Json到前台的两种接收方法推荐

aspx后台传递Json到前台的两种接收方法推荐

2020-01-12 13:47jingxian ASP.NET教程

下面小编就为大家带来一篇aspx后台传递Json到前台的两种接收方法推荐。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

第一种:前台接收

?
1
2
3
4
5
dataType: "json",
 success: function (data)
 {
    var varReceiver = data;
 }
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<%@ 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>

第二种:前台接收

?
1
2
3
4
5
6
dataType: "text",
        success: function (data) {
          //var varReceiver = data;
          var varReceiver = jQuery.parseJSON(data);
。。。。。
}

两者统一的后台 一般处理程序ashx:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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到前台的两种接收方法推荐就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

延伸 · 阅读

精彩推荐