Hello everyone! I’m trying to understand why my jQuery doesn’t work with AJAX (which I need to remove lines from a SQL database. I have included jQuery (not the slim version, so that is not the issue) and it still tells me that ajax is not a function when I type something like
$.ajax({
url:'../db/delete.php',
method: 'POST',
data: {id: id},
success: function() { // Se la richiesta ha successo, voglio rimuovere la riga con id id
for(let i=0; i<id.length; i++) {
$('tr#' + id[i] + '').css('background-color', 'var(--cdoDarkGreen)');
$('tr#' + id[i] + '').fadeOut('slow');
}
}
});
Did anybody else have the same issue recently?
If it is telling you that ajax is not a function, then it is coming back undefined which means that the jQ object (for which “$” is a synonym) is not defined. Try something on the line above:
console.log('jQuery =', $);
You should see an object with a bunch of functions, including ajax. If you see undefined
then there is something wrong with you you imported it incorrectly.
OK, not a big object. When I go into codepen, this is what I get:
console.log('jQuery =', $);
// jQuery = ƒ (e,t){return new k.fn.init(e,t)}
console.log('jQuery.ajax =', $.ajax);
// jQuery.ajax = ƒ (e,t){"object"==typeof e&&(t=e,e=void 0),t=t||{};var c,f,p,n,d,r,h,g,i,o,v=k.ajaxSetup({},t), // ...
SOLVED.
In my structure I had defined a header.php and a footer.php.
I was including every JS script (including jQuery full) in my header, without realizing I included the slim version in the footer (it’s a project I opened again after a few months, so I forgot some things
). I fixed it and now it works alright.
I leave the thread here for other people’s reference.