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?

2 Answers 2


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:


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


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

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

{% block normal %}
    <p>Normal entry</p>
{% 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.

  • Awesome, that worked great! Commented Jun 26, 2014 at 19:45

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" %}

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.