I'm trying to make a Discord Bot, only some features of jQuery works; ajax doesn't

Viewed 355

So, I'm trying to make a (my first) Discord Bot. One command I'm coding is that each time a command is run, it adds a random number (500-1000) to a database (https://keyvalue.immanuel.co//). I tested out the ajax command for jQuery on a small testing Replit, and it worked just fine. However, when I run THE EXACT SAME CODE on Visual Studio:

$.ajax({
    type: "GET",
    url: `https://keyvalue.immanuel.co/api/KeyVal/GetValue/${database_key}/balance`,
    success: function (response) {
        message.channel.send(`Success! ${response} points was added to the world sum!`
    }
});

It gives me an error:

ReferenceError: $ is not defined

I'm so frustrated, because the following code from jQuery DOES work on my module, only ajax doesn't!

console.log(`I can use jquery to do the easy concatenation thing ${database_key}`)

And one last thing to remember: I ran the exact same AJAX code on Replit and it worked.

1 Answers

Remember to reference your scripts/cdn to jquery before the utilization of jquery itself.

You can use googles by including:

<head>
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>

And as @Jon P said, in this case it is much much better to fetch the results using the fetch function instead of an ajax function. An example of such code is listed below.

fetch(`https://keyvalue.immanuel.co/api/KeyVal/GetValue/${database_key}/balance`)
.then(res => res.text())
.then(response => message.channel.send(`Success! ${response} points was added to the world sum!`)
Related