<p>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.</p>

<p>For example, a popular method is to use <a href="https://api.jquery.com/jquery.ajax/" rel="nofollow">jQuery's ajax method</a>.</p>

CC BY-SA 3.0
2016-08-08T17:14:41.970Z
2016-08-08T17:14:41.970Z
Brad Bell
<p>For search autocomplete functionality, you can use <a href="https://github.com/craftcms/element-api" rel="nofollow noreferrer">Element API</a> plugin. It creates API endpoint that can be queried with ajax and it will return JSON (or any other format) results.</p>

<p>On my blog you will find how to implement search autocomplete functionality this way:
<a href="http://craftsnippets.com/articles/search-autocomplete-component-for-craft-cms" rel="nofollow noreferrer">http://craftsnippets.com/articles/search-autocomplete-component-for-craft-cms</a></p>

CC BY-SA 4.0
2019-03-30T10:44:45.913Z
2019-03-30T10:44:45.913Z
Piotr Pogorzelski
<p>You'll need to set the X-Requested-With header to XMLHttpRequest.</p>

<pre><code>const response = await fetch(url, {
  headers: {
    'X-Requested-With': 'XMLHttpRequest',
  },
});
</code></pre>

CC BY-SA 4.0
2020-06-15T06:11:46.467Z
2020-06-15T06:11:46.467Z
Oleg
<p>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.</p>

<ajax>
How can I use Ajax function in Craft CMS?
# How can I use Ajax function in Craft CMS?

(Asked by: username_0 on 2016-08-08)

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.

**Tags:** ajax

---

## 3 answers

### Answer by username_1 on 2016-08-08. Score: 4

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](https://api.jquery.com/jquery.ajax/).


---

### Answer by username_2 on 2019-03-30. Score: 2

For search autocomplete functionality, you can use [Element API](https://github.com/craftcms/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>


---

### Answer by username_3 on 2020-06-15. Score: 1

You'll need to set the X-Requested-With header to XMLHttpRequest.
const response = await fetch(url, {
  headers: {
    'X-Requested-With': 'XMLHttpRequest',
  },
});


---

