因为我是用json格式,所以在使用一般处理程序是需要添加一个NuGet程序包,叫做Newtonsoft.Json点击管理NuGet程序包到浏览里面搜索并安装即可
接下来进入正题,前台代码如下
<script>
echart = echarts.init(document.getElementById("aa"))
$("#Button1").click(function () {
var m = 3;
$.ajax({
type:"post",
url: 'Handler1.ashx',
data: { month: m },//传给ashx文件的值,month是键,m是值
success: function (res) {
var name = []
var num = []
res.forEach(item => {
name.push(item.tbGoods.tbType.Tname)
num.push(item.Ototal)
})
var option = {
title: {
text: '五月销售量'
},
xAxis: {
type: 'category',
data: name
},
tooltip: {},
yAxis: {
type: 'value'
},
series: {
data: num,
type: 'bar',
showBackground: true,
backgroundStyle: {
color: 'rgba(180,180,180,0.2)'
}
}
}
echart.setOption(option);
}
});
})
</script>
ashx文件代码如下
using Newtonsoft.Json;
namespace Supermarket_system
{
/// <summary>
/// Handler1 的摘要说明
/// </summary>
public class Handler1 : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/json";
int m = int.Parse(context.Request["month"]);//通过context.Request[]获取传递过来的值
List<TbOrderDetails> list = OrderDetailsBLL.GettypeSeriesdate(m);//bll层查询数据的方法,m作为参数
string obj = JsonConvert.SerializeObject(list);//将list集合转成json类型
context.Response.Write(obj);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
如果JsonConvert.SerializeObject(list)这里报错,就回头看看有没有using Newtonsoft.Json;