i’m new to javascript and jquery and as part of a college project i need to make periodic checks the database to see if there has been changes. if there has been changes, i want the result of that change to ‘alert’ to the screen just to prove the AJAX call is working. i’m using asp.net and c#.
my scenario is: user ‘A’ logs in and sees user ‘B’ in a listbox populated by a global list. ‘A’ clicks on ‘B’ and clicks the play button. as this happens ‘B’s’ invitedBy column in the User table in the DB changes to ‘A’. this is where my problem is, i want to have a script that periodically accesses a web method in my UserDAO.cs class – which has a method that queries the invitedBy column in the DB (this has been tested and works). i’m having trouble getting this working, would someone take a look and see if they can spot anything. all help would be much appreciated!
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;
}
i’ve been following online tutorials and looking at Qs similar to mine to get this far, so please let me know if i’m on the right track, or offer a solution if you see any problems.
Call your .aspx page, not the code behind.
url: "UserDAO.aspx/queryInvitedBy",