【发布时间】:2012-03-20 20:10:10
【问题描述】:
我是 javascript 和 jquery 的新手,作为大学项目的一部分,我需要定期检查数据库以查看是否有更改。如果有更改,我希望该更改的结果向屏幕“警告”,以证明 AJAX 调用正在工作。我正在使用 asp.net 和 c#。
我的场景是:用户“A”登录并在由全局列表填充的列表框中看到用户“B”。 “A”点击“B”并点击播放按钮。当这种情况发生时,数据库中用户表中的“B”被邀请列更改为“A”。这就是我的问题所在,我希望有一个脚本可以定期访问我的 UserDAO.cs 类中的 web 方法——它有一个查询数据库中的被邀请列的方法(这已经过测试并且有效)。我无法让这个工作,有人可以看看,看看他们是否能发现任何东西。非常感谢所有帮助!
DBPolling.js
$(document).ready(function () {
function ajaxRequest() {
$.ajax({
type: "POST",
url: "UserDAO.cs/queryInvitedBy",// is this correct way to input url?
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (resultData) {
//console.log(result);// i found this in an example...needed?
if (resultData.status == 'pending') {
setTimeout(function () { ajaxRequest(); }, 5000); // wait 5 seconds than call ajax request again
} else {
var result = eval(resultData);
alert("You have been invited" + result);
}
}
});
}
});
UserDAO.cs
[WebMethod]
public string queryInvitedBy()
{
// New user
User user;
// Open the NHibernate session
ISession session = NHibernateHttpModule.CurrentSession;
IQuery q = session.CreateQuery(
"FROM User WHERE InvitedBy IS NOT NULL");
// Assign values to the ? placehoders, by their index (0,1, etc.)
// Make sure List is not empty
if (q.List<User>().Count > 0)
// set user to first item in List
user = q.List<User>()[0];
else
// set user to null if none found
user = null;
// If no users found, returned user will be blank, otherwise the valid user
string result = user.InvitedBy.ToString();
return result;
}
我一直在关注在线教程并查看与我类似的 Q 以达到这一点,所以如果我走在正确的轨道上,请告诉我,或者如果您发现任何问题,请提供解决方案。
【问题讨论】:
-
+1 获取详细的问题和完善的代码
标签: c# jquery asp.net ajax json