I want to do an autocomplete search engine on my website. But I don't know how to use Ajax in Craft CMS. How can I set an Ajax function in Craft CMS templating? The search engine must have dynamic auto-complete feature according to title and content.

3 Answers 3


Since Craft makes no assumptions about the front-end content of your website, it's 100% up to you on how you want to implement AJAX functionality on your site. So it wouldn't be any different if you were trying to use AJAX on a simple static page with no CMS managing the content.

For example, a popular method is to use jQuery's ajax method.


For search autocomplete functionality, you can use Element API plugin. It creates API endpoint that can be queried with ajax and it will return JSON (or any other format) results.

On my blog you will find how to implement search autocomplete functionality this way: http://craftsnippets.com/articles/search-autocomplete-component-for-craft-cms


You'll need to set the X-Requested-With header to XMLHttpRequest.

const response = await fetch(url, {
  headers: {
    'X-Requested-With': 'XMLHttpRequest',

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.