aspx后台传递Json到前台的两种接收方法推荐
第一种:前台接收
dataType:"json", success:function(data) { varvarReceiver=data; }
<%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="echart2.aspx.cs"Inherits="RTC.echart2"%> <!DOCTYPEhtml> <htmlxmlns="http://www.w3.org/1999/xhtml"> <headrunat="server"> <metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/> <title></title> <!--引入ECharts文件--> <scriptsrc="scripts/echarts.common.min.js"></script> <scriptsrc="scripts/jquery-1.10.2.min.js"></script> <scriptsrc="scripts/json2.js"></script> </head> <body> <formid="form1"runat="server"> <!--为ECharts准备一个具备大小(宽高)的Dom--> <divid="main"style="width:1000px;height:400px;"> </div> <scripttype="text/javascript"> varvarAxis; varvarSeries; //varvarRtcNO=$("#txtHid").val(); varvarRtcNO="35000002818"; varjdata; varmyChart=echarts.init(document.getElementById('main')); //显示标题,图例和空的坐标轴 myChart.setOption({ title:{ text:'温度曲线图' }, tooltip:{ trigger:'axis', axisPointer:{ animation:false }, formatter:function(params){ returnparams[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){ varvarReceiver=data; //varvarReceiver=jQuery.parseJSON(data); varvarAxis=newArray(); varvarSeries=newArray(varReceiver.Count[0].total); for(vari=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){ //varvarReceiver=data; varvarReceiver=jQuery.parseJSON(data); 。。。。。 }
两者统一的后台一般处理程序ashx:
usingSystem; usingSystem.Collections.Generic; usingSystem.Data; usingSystem.Data.SqlClient; usingSystem.Linq; usingSystem.Web; namespaceRTC { ///<summary> ///getrtchistorydata的摘要说明 ///</summary> publicclassgetrtchistorydata:IHttpHandler { publicvoidProcessRequest(HttpContextcontext) { context.Response.ContentType="text/plain"; stringstrRTCNo=context.Request.QueryString["rtcno"].ToString(); SqlConnectioncon=newSqlConnection("server=192.168.0.222;uid=sa;pwd=hiwits;database=CeShi_QingDao;MaxPoolSize=2048;"); SqlCommandcmd=newSqlCommand("selectRtcNO,RoomTemp,InstallPlace,convert(varchar,RecordTime,120)asRecordTime,systimefromRTCHistorywhereRtcNO='"+strRTCNo+"'orderbyInstallPlace,RecordTime",con); SqlDataAdapterda=newSqlDataAdapter(cmd); DataSetds=newDataSet(); da.Fill(ds); stringstbList=""; stbList="{\"Rows\":["; foreach(DataRowdrinds.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()); } publicboolIsReusable { get { returnfalse; } } publicvoidRetrunHistoryData() { } } }
以上这篇aspx后台传递Json到前台的两种接收方法推荐就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。