I have this problem where the jQuery get
function inside my updateMyApps
isn't being called correctly. I call the function right after declaring it and that works perfectly. It loops through the data and appends the elements to the DOM.
But when I submit the form #addapplicationform
it runs the jQuery post function which works correctly but the updateMyApps
function doesn't seem to be working correctly when that happens.
The console.log("Test");
always seems to get called. Even when submitting the form. But for some reason the callback function for that get request only gets called that one time on page load.
<script>
function updateMyApps() {
console.log("Test"); //Always works
$.get("/api/clients", function(data) {
console.log(data); //Works first time
$("#myapps").html("<h4>My Applications</h4>");
data.forEach(function (item) {
var element = '<div>' + item.name + '</div>' + '<div>' + item.id + '</div>' + '<div>' + item.secret + '</div>' + '<a href="/edit"><input type="submit" class="submit btn btn-primary form-control" value="Edit" style="width: 100px;"></a>';
$("#myapps").append(element);
});
});
}
updateMyApps(); // Works perfectly
$("#addapplicationform").submit(function( event ) {
event.preventDefault();
$.post("/api/clients", $( "#addapplicationform" ).serialize(), function () {
updateMyApps(); //Doesn't work
});
});
</script>
Any ideas on why the get request callback is only firing once?
EDIT
Forgot to mention this. It looks like the GET /api/clients
only is getting called on page load. When submitting the form it isn't even hitting my back end at all.
EDIT 2
Realized this was a caching issue. Adding $.ajaxSetup({ cache: false });
seems to have fixed the issue. Also marked the question as duplicate of where I found that code snippet.