<p>There is a new RESTful plugin which might be what you need: <a href="https://github.com/wesrice/rest-easy">Rest-Easy from Wes Rice</a>.<br>
But you could also make use of Twig's <a href="http://twig.sensiolabs.org/doc/tags/extends.html">extends functionality</a>:</p>

<p><code>_ajax.html</code>:</p>

<pre class="lang-twig prettyprint-override"><code>{% block ajax %}
    { success: false }
{% endblock %}
</code></pre>

<p><code>entry.html</code>:</p>

<pre class="lang-twig prettyprint-override"><code>{% extends craft.request.isAjax ? "_ajax" : "_main" %}

{% block ajax %}
    { success: true }
{% endblock %}

{% block normal %}
    &lt;p&gt;Normal entry&lt;/p&gt;
{% endblock %}
</code></pre>

<p>If you request <code>entry.html</code> via ajax, the template will extend <code>_ajax.html</code> instead of <code>_main.html</code>, and show your json data. In this example <code>_main.html</code> would be your normal layout-template.</p>

<p>Note: Don't forget to set the <code>XMLHttpRequest</code> in your ajax call.</p>

CC BY-SA 3.0
2014-06-26T19:45:45.260Z
Awesome, that worked great!
carbonjoetunis
CC BY-SA 3.0
2014-06-26T19:05:35.187Z
2014-06-26T19:05:35.187Z
Victor
<p>You do not need a plugin. You just need to write a template (which may require you to define a custom route in the Control Panel) that returns JSON. You can even feed it GET or POST values and use those as the filters.</p>

<p>To have the template be recognized as JSON, you just need to give the template a '.json' extension. If that doesn't work you can force it with:</p>

<p><code>{% header "Content-Type: application/json" %}</code></p>

CC BY-SA 3.0
2014-06-26T19:07:41.650Z
2014-06-26T19:07:41.650Z
Bryan Redeagle
<p>I want to load some entries via AJAX. I have a page that normally shows <em>all</em> entries of a specific type with user-selectable filters. I want to load filtered entries via AJAX. </p>

<p>Do I need to create a plugin to do this? If so, how can I use the Craft object in my JavaScript code to call Craft.ajaxActionRequest or Craft.postActionRequest?</p>

<entries><ajax>
Do I need a plugin to load entries via AJAX?
# Do I need a plugin to load entries via AJAX?

(Asked by: username_0 on 2014-06-26)

I want to load some entries via AJAX. I have a page that normally shows *all* entries of a specific type with user-selectable filters. I want to load filtered entries via AJAX.

Do I need to create a plugin to do this? If so, how can I use the Craft object in my JavaScript code to call Craft.ajaxActionRequest or Craft.postActionRequest?

**Tags:** entries, ajax

---

## 2 answers

### Answer by username_1 on 2014-06-26. Score: 10

There is a new RESTful plugin which might be what you need: Rest-Easy from Wes Rice.
But you could also make use of Twig's extends functionality:
_ajax.html:
{% block ajax %}
    { success: false }
{% endblock %}

entry.html:
{% extends craft.request.isAjax ? "_ajax" : "_main" %}

{% block ajax %}
    { success: true }
{% endblock %}

{% block normal %}
    Normal entry
{% endblock %}

If you request entry.html via ajax, the template will extend _ajax.html instead of _main.html, and show your json data. In this example _main.html would be your normal layout-template.
Note: Don't forget to set the XMLHttpRequest in your ajax call.

> **Comments:**
> **username_0**: Awesome, that worked great!  

---

### Answer by username_2 on 2014-06-26. Score: 2

You do not need a plugin. You just need to write a template (which may require you to define a custom route in the Control Panel) that returns JSON. You can even feed it GET or POST values and use those as the filters.

To have the template be recognized as JSON, you just need to give the template a '.json' extension. If that doesn't work you can force it with:

`{% header "Content-Type: application/json" %}`


---

