Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-05T18:13:45.363Z
Answers.Text: 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.
Answers.UserDisplayName: Subrato M
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-05T18:27:08.897Z
Answers.Text: 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?
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-05T18:30:30.763Z
Answers.Text: 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?
Answers.UserDisplayName: Subrato M
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-05T18:37:55.980Z
Answers.Text: 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.
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-05T18:48:15.143Z
Answers.Text: 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.
Answers.UserDisplayName: Subrato M
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-05T21:13:15.353Z
Answers.Text: @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.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-05T17:11:40.343Z
Answers.LastActivityDate: 2016-07-05T17:11:40.343Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <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>
  
Tags: <ajax><layout>
Title: Ajax layout sample
ThreadText: # 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**: @username_0 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.  
  
  ---
  
  