I'm building a static webpage with php and Javascript. I have setup all my data in craft cms entries. I want to use ajax to retrieve the data from craft and just display it on my webpage. How do I do this?
1 Answer
You'll need to expose some sort of API that your JavaScript can use to request data from your Craft install.
There are two first-party options:
Craft CMS has had GraphQL support built into core since version 3.3.0. The official docs provides a fairly comprehensive guide to get up and running.
If you prefer a RESTful JSON API, the first-party Element API plugin provides a simple interface to configure your endpoints. The documentation for Element API can be found on the plugin's GitHub page. The Element API plugin is available for both Craft 3 and 4.
Aside from the above, if you happen to use Gatsby for your site there's also a first-party Gatsby source plugin for Craft.
Finally, when using Craft as a CMS for a headless app/site, it's also worth knowing about the headlessMode
, which "optimizes the system and control panel for headless CMS implementations".