Nick_M Nick_M - 1 year ago 61
Javascript Question

Re-run the Last AJAX Call Made

I am currently trying to refresh a small section of my application which is being generated by an AJAX call, I need to be able to target the last call that was made and run it again with the same parameters and run through a different button click. The way it received the data before was from the previously clicked button. I am working in MVC with C# on the back-end. Here is my AJAX call:

$('.eventlevel').on('click', function (e) {

type: "GET",
url: "/Home/GetTasksForEventLevel",
data: {
EventLevelID: $(this).attr('data-EventLevelID')
success: function (eventLevel) {
$('#tasks').html(' ');
$.each(, function (key, value) {
var html = " <div class='dashbox col-lg-5 col-sm-5 col-xs-5'>";
if (value.EventLevel == 3) {
html += "<div class='green-circle'></div>";
else if (value.EventLevel == 2) {
html += "<div class='yellow-circle'></div>";
else if (value.EventLevel == 1) {
html += "<div class='red-circle'></div>";
else if (value.EventLevel == 4) {
html += "<div class='grey-circle'></div>";
html += "<p class='bodytext'>" + value.EventDescription + "</p>";
html += "<p class='bodytext'>Pending Items: (" + value.EventValue + ")</p>";
html += "<p class='bodytext'>Last Updated: " + value.EventDateTimeText + "</p>";
html += "<p><a href='#'>View History</a></p>";
html += "<button data-MonitoringEventID='" + value.MonitoringEventID + "' class='raise'>Raise Alert</button>";
html += "<button class='review'>Commit Review</button> </div>";
return false;

Thanks in advance for all help that is offered.

Answer Source

I'd recommend an ajax wrapper function that would accept the ajax object as a parameter and store that in a variable to keep track of the "last" call. Something like this should work:

var lastAjaxCall;
function ajaxWrapper(obj){
    lastAjaxCall = obj;

A function calling your last ajax call would be as simple as the following:

function executeLastAjaxCall(){
    if(lastAjaxCall) $.ajax(lastAjaxCall);