heml Ajax 通过Asp.net访问数据库(以Sql Server为例)
发布时间:2020-12-15 21:59:26 所属栏目:百科 来源:网络整理
导读:假设从数据库提取数据,在html页面中显示: 1,建立服务器后端处理程序 添加“一般处理程序”,如下图 650) this.width=650;" width="954" height="658" title="捕获.PNG" style="width:606px;height:467px;float:left;" alt="220824766.png" src="http://im
假设从数据库提取数据,在html页面中显示: 1,建立服务器后端处理程序 添加“一般处理程序”,如下图 2.编写“Handler2.ashx”代码,如下: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Script.Serialization; namespace WebApplication5 { /// <summary> /// Handler1 的摘要说明 /// </summary> public class Handler1 : IHttpHandler { public void ProcessRequest(HttpContext context) { //采用EF从数据库中提取数据 WeatherDBEntities weatherDbcontext = new WeatherDBEntities(); var dataset = from data in weatherDbcontext.T_Station select new { data.StationName,data.StationPosition,data.Lat,data.Lon }; JavaScriptSerializer tool = new JavaScriptSerializer(); context.Response.ContentType = "text/plain"; //字符串形式 context.Response.Write(tool.Serialize(dataset));//将字符串编码为JSON类型 // //备注,要从javascript中传参数的话,如下代码,data就是传递的参数。而在本文件中,通过context.request("zipcode")来获取 //如context.Response.Write(context.Request.Params["zipcode"]); // $.ajax({ // url: "/api/getWeather",//data: { // zipcode: 97201 //},// success: function( data ) { // $( "#weather-temp" ).html( "<strong>" + data + "</strong> degrees" ); // } //}); } public bool IsReusable { get { return false; } } } } 3.添加Html页面:HtmlPage1.html 4.结果如下 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |