我是 javascript 和 jquery 的新手,作为大学项目的一部分,我需要定期检查数据库以查看是否有更改。如果有更改,我希望更改的结果“提醒”屏幕以证明 AJAX 调用正在运行。我正在使用 asp.net 和 c#。
我的场景是:用户“A”登录并在由全局列表填充的列表框中看到用户“B”。 “A”点击“B”并点击播放按钮。发生这种情况时,数据库中用户表中的“B”的 invitedBy 列更改为“A”。这就是我的问题所在,我想要一个脚本来定期访问我的 UserDAO.cs 类中的 Web 方法——它有一个查询数据库中 invitedBy 列的方法(这已经过测试并且有效)。我无法正常工作,有人会看一看他们是否能发现任何东西吗?非常感谢所有帮助!
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;
}
我一直在关注在线教程并查看与我类似的问题以取得目前的进展,所以请让我知道我是否在正确的轨道上,或者如果您发现任何问题,请提供解决方案。
最佳答案
调用您的 .aspx 页面,而不是后面的代码。
url: "UserDAO.aspx/queryInvitedBy",
关于c# - ajax jquery post,需要对初学者进行一些微调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9794160/