I am creating a game, and want to fetch data from api using Ajax. But when i add ajax code it gives error that $ is not supported, but ajax cannot work without $. How can i then get data without refreshing whole html document
Most likely you are trying to use jQuery, hence the $ error. If you plan to use jQuery you need to add that library to your PlayCanvas project.
But it’s not required to do Ajax/Rest calls, you can use the browser fetch method to retrieve remote data. For example here is a sample Rest call to retrieve JSON using a GET request:
const response = await fetch('http://remote.url'); const json = await response.json();
how to add refernce to jquery library, because i had already the refrence using
Go to your project settings and in the External Scripts tab, you can add there a remote absolute url with the jQuery library. They provide several CDN based urls:
Otherwise you can drag and drop the jquery .js file in your PlayCanvas assets panel.
Thanks for your help, it is working now