Skip to content Skip to sidebar Skip to footer

"$.ajax Is Not A Function", But Only If Call Is Placed Inside Function

It's been a while since I've done web programming now, and managed to get myself into some trouble. Probably a trivial thing to solve for someone, but I've searched quite a bit onl

Solution 1:

This tells us that some script code later in the file is either:

  1. Calling jQuery's noConflict function, which releases the $ identifier, or

  2. Including some script that overwrites the value in $ (for instance: MooTools.js, Prototype.js, or even jQuery itself in its "slim" build)

You can solve it by wrapping your code in a function that uses its own $, like this:

(function($) {
    // ...your code here, can use`$` without worrying...
})(jQuery); // <==  Passes in `jQuery`, which is a synonym for`$`

Even if some later code calls noConflict, or even completely overwrites $ (or even jQuery), that code will continue to work because it grabs the value of jQuery as of when it runs.

Post a Comment for ""$.ajax Is Not A Function", But Only If Call Is Placed Inside Function"