echarts html传参+js请求+ashx服务 代码方式

时间:2023-03-09 02:10:12
echarts html传参+js请求+ashx服务 代码方式

html 头传参方式

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script src="/Scripts/jquery-2.1.1.min.js"></script>

    <!-- ECHART -->
    <script type='text/javascript' src="/Scripts/esl.js"></script>
    <script type='text/javascript' src="/Scripts/echarts.js"></script>

    <script src="/Scripts/Main.js"></script>
    <script src="/Scripts/RealTimeView.js"></script>
</head>
<body>
    <div class="tab-pane" id="tab2">
        <div id="tab1chart1" class="row" style="height: 300px"></div>
        <div id="tab1chart2" class="row" style="height: 300px"></div>
    </div>
</body>
</html>
<script>
    $(function () {
        btnr();
    });
</script>

js 处理html参数,并向后台请求

var arg1;
var arg2;

var LocString=String(window.document.location.href);   

function getQueryStr(str){
    var result = location.search.match(new RegExp("[\?\&]" + str + "=([^\&]+)", "i"));
    if (result == null || result.length < 1) {
        return "";
    }
    return result[1];
}

//获取HTTP参数
function receiveParameter() {
    arg1 = getQueryStr("arg1");
    arg2= getQueryStr("arg2");
}
    $.ajax({
        "dataType": 'json',
        "type": "GET",
        async: false,
        "url": '/Info.ashx',
        "data": { 'arg1': arg1, "arg2": arg2 },
        "success": function (json) {
            if (json == null) {
                myChart1.showLoading({
                    //返回数据为空
                    text: ""
                });
                return;
            }
            options.xAxis[0].data = json.category;
            options.series = json.series;
            options.legend.data = json.legend;
            options.title.text = '曲线';
            myChart1.hideLoading();
            myChart1.setOption(options);
        },
        error: function (errorMsg) {
            alert(errorMsg);
        }
    });

ashx服务返回,经过JavaScriptSerializer转化为JSON

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                Object infoList =InfoService.GetChart(arg1, arg2);
                JavaScriptSerializer js = new JavaScriptSerializer();
                context.Response.ContentType = "text/plain";
                context.Response.Write(js.Serialize(infoList));
            }
            catch
            {
                context.Response.ContentType = "text/plain";
                context.Response.Write("error");
            }
        }