I have an Ajax success request and I would like to take the results of the Ajax success function and use it wherever I want in the code. How can I achieve this?
code
$.ajax({
url: ajaxurl,
data: {
action: "showDbValues",
},
success: function(response) {
let getLastTime = response.data.task_time;
let getLastTitle = response.data.task_title;
return getLastTime;
},
error: function(errorThrown) {
window.alert(errorThrown);
},
});
let lastTime = (document.querySelector(".c-add-task-show-time").innerHTML = `${getLastTime}`);
console.log(lastTime);
You could create a variable before the ajax function called foobar. Then set the return value of the Ajax call equal to foobar. As foobar was created outside the scope of the ajax function you should be able to reuse via the foobar variable.