处理程序:
public void ProcessRequest(HttpContext context)
{
// context.Response.ContentType = "text/plain";
// context.Response.Write("Hello World");
DataTable dt = DAL.RoomInfo.RoomInfos();
List room = new List();
foreach (DataRow dr in dt.Rows)
{
Model.Rooms u = new Model.Rooms();
u.Rid = Convert.ToInt16(dr["rid"]);
u.Rprice = dr["rprice"].ToString();
u.Rtype = dr["rtype"].ToString();
u.Toward = dr["toward"].ToString();
room.Add(u);
}
JavaScriptSerializer jv = new JavaScriptSerializer();
string s = jv.Serialize(jv);
context.Response.Write(s);
}
页面部分:
$(function () {
$.ajax({
type: "get",
url: "/Handler/GetRoomInfo.ashx",
dataType: "json",
success: function (data)
{
var s = "";
$.each(data, function (index,o) {
s+="",
s+=""+o.rid+"";
s+=""+o.rprice+"";
s+=""+o.rtype+"";
s += "" + o.toward + "";
s += "";
})
$("#RoomInfo tr:gt(0)").remove();
$("#RoomInfo").append(s);
}
})
})
用fiddler调试下,看你的服务器返回了什么状态,返回的json是否正确,还是反悔了什么错误状态。
增加下面3句alert看都执行没有。一个都没执行说明jquery没有正确导入,或者执行这个代码之前的代码报错了导致没执行,如果执行了ajaxSendEnd,没执行SUCCESS,那就是你动态页有问题
$(function () {
alert('ajaxSend')//////////
$.ajax({
type: "get",
url: "/Handler/GetRoomInfo.ashx",
dataType: "json",
success: function (data) {
alert('SUCCESS')//////////
var s = "";
$.each(data, function (index, o) {
s += "",
s += "" + o.rid + "";
s += "" + o.rprice + "";
s += "" + o.rtype + "";
s += "" + o.toward + "";
s += "";
})
$("#RoomInfo tr:gt(0)").remove();
$("#RoomInfo").append(s);
},error:function(xhr){alert(xhr.responseText)}//执行error看服务器返回什么内容
})
alert('ajaxSendEnd')////////
})