I am new to javascript and jquery, and as part of a college project, I need to periodically check the database to see if there have been changes. if a change has occurred, I want the result of this change to be a βwarningβ on the screen to prove that the AJAX call is working. I am using asp.net and c #.
my scenario: user βAβ logs in and sees user βBβ in a list populated with a global list. βAβ presses βBβ and presses the play button. as this happens, column βBβ is invited to By in the table βUserβ in the database changes to βAβ. this is my problem, I want to have a script that periodically accesses the web method in my UserDAO.cs class, which has a method that queries the inviteBy column in the database (this has been tested and works). Iβm having trouble getting this job, someone will take a look and see if they can find anything. All help would be greatly 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 followed online tutorials and looked at Qs like mine to go that far, so please let me know if I'm on the right track, or offer a solution if you see any problems.
source share