<p>You're not specific about what your Ajax template should return (HTML, JSON...?) – but assuming you want your payload in JSON format, here's a basic layout example which will 1) test if the current request is indeed an Ajax request and 2) set headers for JSON and Access-Control-Allow-Origin (crossdomain):</p>

<p>In <code>/templates/_layouts/ajax</code>:</p>

<pre><code>{% if not craft.request.isAjaxRequest %}
    {% exit 404 %}
{% endif %}
{% header "Content-Type: application/json; charset=utf-8" %}
{% header "Access-Control-Allow-Origin: *" %}
{% block content %}{% endblock %}
</code></pre>

<p>In <code>/templates/api/sometemplate</code>:</p>

<pre><code>{% extends '_layouts/ajax' %}
{% block content %}
    {
        "foo": "bar",
        "baz": "qux"
    }
{% endblock %}
</code></pre>

CC BY-SA 3.0
2016-07-05T18:13:45.363Z
I am looking for a ajax layout itself. So basically _ajax.html. I have no idea what it should look like.

Also thank you for the example because I needed that too.
Subrato M
CC BY-SA 3.0
2016-07-05T18:27:08.897Z
TBH I'm not sure what you mean by "ajax layout". Are you referring to the actual JavaScript? It might be helpful if you added some more info to your question - what, exactly, are you trying to achieve?
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2016-07-05T18:30:30.763Z
So honestly I am not sure how to achieve an ajax layout. CraftCMS has a standard layout which I modified to use but if I have to use ajax calls on the page, what do I add to the layout? What javascript would I need to add?
`{% if not craft.request.isAjaxRequest %}
    {% exit 404 %}
{% endif %}
{% header "Content-Type: application/json; charset=utf-8" %}
{% header "Access-Control-Allow-Origin: *" %}
{% block content %}{% endblock %}`

Would this be what would make a layout ajax template?
Subrato M
CC BY-SA 3.0
2016-07-05T18:37:55.980Z
Ajax is just a technique for client-server communication, and there's not really a thing called "ajax layout". You can use the stock templates out of the box with Ajax, though the payload will be HTML. As it stands, I'm afraid your question is way too broad, and its impossible to answer it without knowing more about your goal or use case - are you looking to ajaxify your whole site, do you want an "infinite scroll" thing, or something else? In other words; what do you want to use Ajax for? There's tons of ways to do it and Craft doesn't impose a certain way. It depends on the use case.
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2016-07-05T18:48:15.143Z
I want ajax to just fetch data without complete postback. Or in other case where people click on a radio button and then a bunch of options show up which don't if you select the other one.
Subrato M
CC BY-SA 3.0
2016-07-05T21:13:15.353Z
@SubratoM Just use jQuery's AJAX methods. https://learn.jquery.com/ajax/jquery-ajax-methods/ If you're coming from .NET WebForms land, you're probably used to the framework automagically doing all of this stuff for you. There's really nothing like that in the PHP framework world.
Brad Bell
CC BY-SA 3.0
2016-07-05T17:11:40.343Z
2016-07-05T17:11:40.343Z
Mats Mikkel Rummelhoff
<p>I am just learning how to use this CMS and I am able to get basic things done but I have no idea what a ajax layout will look like. I have few pages in old system which fetch info using ajax calls (.net based system so it was very easy). </p>

<p>I don't know how a layout in this CMS should look like</p>

<ajax><layout>
Ajax layout sample
# Ajax layout sample

(Asked by: username_0 on 2016-07-05)

I am just learning how to use this CMS and I am able to get basic things done but I have no idea what a ajax layout will look like. I have few pages in old system which fetch info using ajax calls (.net based system so it was very easy).

I don't know how a layout in this CMS should look like

**Tags:** ajax, layout


---

## 1 answers

### Answer by username_1 on 2016-07-05. Score: 3 | Accepted answer

You're not specific about what your Ajax template should return (HTML, JSON...?) – but assuming you want your payload in JSON format, here's a basic layout example which will 1) test if the current request is indeed an Ajax request and 2) set headers for JSON and Access-Control-Allow-Origin (crossdomain):
In /templates/_layouts/ajax:
{% if not craft.request.isAjaxRequest %}
    {% exit 404 %}
{% endif %}
{% header "Content-Type: application/json; charset=utf-8" %}
{% header "Access-Control-Allow-Origin: *" %}
{% block content %}{% endblock %}

In /templates/api/sometemplate:
{% extends '_layouts/ajax' %}
{% block content %}
    {
        "foo": "bar",
        "baz": "qux"
    }
{% endblock %}

> **Comments:**
> **username_0**: I am looking for a ajax layout itself. So basically _ajax.html. I have no idea what it should look like.  Also thank you for the example because I needed that too.  
> **username_1**: TBH I'm not sure what you mean by "ajax layout". Are you referring to the actual JavaScript? It might be helpful if you added some more info to your question - what, exactly, are you trying to achieve?  
> **username_0**: So honestly I am not sure how to achieve an ajax layout. CraftCMS has a standard layout which I modified to use but if I have to use ajax calls on the page, what do I add to the layout? What javascript would I need to add? `{% if not craft.request.isAjaxRequest %}     {% exit 404 %} {% endif %} {% header "Content-Type: application/json; charset=utf-8" %} {% header "Access-Control-Allow-Origin: *" %} {% block content %}{% endblock %}`  Would this be what would make a layout ajax template?  
> **username_1**: Ajax is just a technique for client-server communication, and there's not really a thing called "ajax layout". You can use the stock templates out of the box with Ajax, though the payload will be HTML. As it stands, I'm afraid your question is way too broad, and its impossible to answer it without knowing more about your goal or use case - are you looking to ajaxify your whole site, do you want an "infinite scroll" thing, or something else? In other words; what do you want to use Ajax for? There's tons of ways to do it and Craft doesn't impose a certain way. It depends on the use case.  
> **username_0**: I want ajax to just fetch data without complete postback. Or in other case where people click on a radio button and then a bunch of options show up which don't if you select the other one.  
> **username_2**: @SubratoM Just use jQuery's AJAX methods. https://learn.jquery.com/ajax/jquery-ajax-methods/ If you're coming from .NET WebForms land, you're probably used to the framework automagically doing all of this stuff for you. There's really nothing like that in the PHP framework world.  

---

