Ajax jquery post, needs some tweaking for a beginner

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.

+4
source share
2 answers

Call your .aspx page, not the code.

url: "UserDAO.aspx/queryInvitedBy",

+3
source

as far as I know, your problem calls the function again and again.

you can define global timer variable in javascript

 var timer; 

and create a function

 function check(){ clearTimeout(timer); //do the ajax thing here timer=setTimeout('check()',5000); } 

now that you want to use the funcion function

 timer=setTimeout('check()',5000); 

and it should work every 5 seconds .....

- This is what you need?

0
source

Source: https://habr.com/ru/post/1402533/


All Articles