五分钟内反应回复,平均十分钟解决任何IT技术问题,按难度跟工作量收费!

关于ajax前台通过ashx后台传值json的问题

reborn |浏览202次
收藏|2018/05/31 18:03
<!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 type="text/javascript" src="/js/jquery-2.1.4.js"></script>
    <script type="text/javascript" src="/js/echarts.js"></script>
</head>
<body>
    <div id="myEchart" style="height:500px;width:800px"></div>
    <script type="text/javascript">
        var myChart1 = echarts.init(document.getElementById('myEchart'));
        var option1 = {
            tooltip: {},
            title: {
                text: '入门示例'
            },
            xAxis: [
                {
                    type: 'value', name: '数据点'
                }
            ],
            yAxis: [
                {
                    type: 'value', name: '幅值'
                }
            ],
            series: [
                {
                    type: 'line',
                    data: []
                }
            ]
        };
        myChart1.setOption(option1);
        var numx = [];
        var numy = [];
        $.ajax({
            type: "post",
            async: false,
            url: "Test0.ashx",
            data: {},
            datatype: "json",
            sucess: function (result){
                for (var i = 0; i < result.length; i++) {
                    numx.push(result[i].num);
                }
                for (var i = 0; i < result.length; i++) {
                    numy.push(result[i].data);
                }
                myChart1.setOption({
                    xAxis: {
                        data: numx
                    },
                    series: [{
                        data: numy
                    }]
                });
            },
            error: function (errorMsg) {
                alert("请求数据失败!");
            }
        });
    </script>

</body>
</html>

```
 //上面是前台代码,主要是实现画折现图的,后台代码在下面
```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Data;
using System.Web.Script.Serialization;

namespace SSF_Echarts_WebApp_01
{
    /// <summary>
    /// Test0 的摘要说明
    /// </summary>
    public class Test0 : IHttpHandler
    {
        MySqlConnection con = new MySqlConnection("Data Source=localhost;User ID=root;Password=root;DataBase=shift;");
        DataSet ds = new DataSet();
        MySqlDataAdapter adapter = new MySqlDataAdapter();
        JavaScriptSerializer jsS = new JavaScriptSerializer();
        List<object> lists = new List<object>();
        //Series seriesObj = new Series();
        string result = "";
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Charset = "utf-8";
            //context.Response.Write("Hello World");
            Get_Data01(context);
        }
        public void Get_Data01(HttpContext context)
        {
            string sql = @"select * from test101";
            ds = GetDataFromDatabase(sql);
            lists = new List<object>();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                var obj = new { num = dr["idtest1"], data = dr["X"] };
                lists.Add(obj);
            }
            jsS = new JavaScriptSerializer();
            result = jsS.Serialize(lists);
            context.Response.Write(result);
        }
        public DataSet GetDataFromDatabase(string sql)
        {
            ds = new DataSet();
            adapter = new MySqlDataAdapter(sql, con);
            adapter.Fill(ds);
            return ds;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

//主要过程是通过sql语句提取得到数据x,y坐标,转换为json数据传递给前台,但前台总是收不到数据,
//在ajax中进行alert不跳框,也不显示失败;本人刚刚开始学习,希望大家能够帮忙,可有偿,谢谢


回答该问题即可获得 2 经验值,问题被采纳即可获得 2 经验值!
我来回答

验证码

  换一个
全部回答(1)
  • 可以处理,费用200
    回答于 2018/06/01 12:17
+1
 加载中...