Track Ajax requests on page

I have a web page and I associate the note with the page that stores it in my database, so that whenever the page is next loaded, the notification can be viewed until it is deleted by another user authorized do the same thing. However, let's say I associate a note with a page after loading some content through AJAX. I want the note to appear only after this specific content is loaded the next time on the web page. I would like to track an AJAX request and attach a note to it? I want to do this through simple JavaScript code placed at the bottom of any web page. How can I do that?

+4
source share
2 answers

jQuery ajax provides methods for triggering events before, during, and after an ajax request. Please view the api here for a complete list.

Here are a few methods that are called after the completion of each ajax request.

jQuery.ajaxComplete () - register a handler that will be called when Ajax requests are completed.

jQuery.ajaxSuccess () - Show a message when an Ajax request completes successfully

These functions allow you to pass a callback function to run after an event occurs. In this callback function, check if the content you want to download is loaded, then show the note.

Sample code for the above methods can be found here:

http://api.jquery.com/ajaxComplete/

http://api.jquery.com/category/ajax/

Hope this helps.

+3
source

You need to write a wrapper for all of your ajax calls that check the result and see if a note should be added. Here is an example, assuming you are using jQuery to handle ajax requests:

function ajaxWrapper(settings){ var old_callback = settings.complete; settings.complete = function(request, status){ if(status == "200"){ // check the request.responseText and add your note if appropriate } if(old_callback){ old_callback(request, status); } } jQuery.ajax(settings); } 

And then instead of calling jQuery.ajax () directly, call ajaxWrapper ().

An alternative would be to include a note in your ajax answer.

0
source

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


All Articles