Body: <p>Ok this has been puzzling me for too long now! I have some code that makes an ajax request:</p>
  
  <pre><code>reqAjax: {
      type: 'get',
      url: '/mytemplatefolder/myfile.html',
      data: {town: 'bournemouth'}
  }
  </code></pre>
  
  <p>This returns a json object to populate dates in a calendar. The dates have links to the town the event is in but to do this dynamically I need to be able to make use of the town param in twig. What I have at the moment at the top of myfile.html is:</p>
  
  <pre><code>{% header "Content-Type: application/json; charset=utf-8" %}
  {% header "Access-Control-Allow-Origin: *" %}
  
  {% set town = craft.request.getParam('town') %}
  </code></pre>
  
  <p>But town is not being set. So I guess I am missing something on how to get the data from the ajax call and set it in the template. Any help much appreciated.</p>
  
  <p>Cheers</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-10-16T20:25:59.693Z
Comments.Text: `craft.request.getParam('town')` is the correct way to pull a GET variable, so your code should work. What happens if you visit the AJAX URL directly in the browser (e.g. `http://domain.com/mytemplatefolder/myfile?town=something`)?
Comments.UserDisplayName: Mats Mikkel Rummelhoff
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-10-17T08:19:08.993Z
Comments.Text: @mmikkel cheers good tip. I can see the json array and it works if I append a query string to the url in the ajax call but not if I try and send it via a param. i.e.
  This works
      url: '/calendar-dates/json-full.html?town=newcastle',
  This Does not
      data: {town: 'bournemouth'}
  I can get it working by appending the query string but wondered why it doesn't work with the param?
Comments.UserDisplayName: Lettie
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-10-17T08:24:01.233Z
Comments.Text: There isn't really anything super special about AJAX calls - a GET is a GET and a POST is a POST. If it works with a query string, the issue must be with your JS. Are you using jQuery?
Comments.UserDisplayName: Mats Mikkel Rummelhoff
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-10-17T08:44:01.727Z
Comments.Text: @mmikkel yes I am. No worries at least it's working. I'll look into the JS a bit closer. Thanks for your help.
Comments.UserDisplayName: Lettie
Tags: <templating><ajax><params>
Title: How to get a param from an ajax request in twig?
ThreadText: # How to get a param from an ajax request in twig?
  
  (Asked by: username_0 on 2015-10-16)
  
  Ok this has been puzzling me for too long now! I have some code that makes an ajax request:
  
  ```
  reqAjax: {
      type: 'get',
      url: '/mytemplatefolder/myfile.html',
      data: {town: 'bournemouth'}
  }
  
  ```
  
  This returns a json object to populate dates in a calendar. The dates have links to the town the event is in but to do this dynamically I need to be able to make use of the town param in twig. What I have at the moment at the top of myfile.html is:
  
  ```
  {% header "Content-Type: application/json; charset=utf-8" %}
  {% header "Access-Control-Allow-Origin: *" %}
  
  {% set town = craft.request.getParam('town') %}
  
  ```
  
  But town is not being set. So I guess I am missing something on how to get the data from the ajax call and set it in the template. Any help much appreciated.
  
  Cheers
  
  **Tags:** templating, ajax, params
  
  > **Comments:**
  > **username_1**: `craft.request.getParam('town')` is the correct way to pull a GET variable, so your code should work. What happens if you visit the AJAX URL directly in the browser (e.g. `http://domain.com/mytemplatefolder/myfile?town=something`)?  
  > **username_0**: @mmikkel cheers good tip. I can see the json array and it works if I append a query string to the url in the ajax call but not if I try and send it via a param. i.e. This works     url: '/calendar-dates/json-full.html?town=newcastle', This Does not     data: {town: 'bournemouth'} I can get it working by appending the query string but wondered why it doesn't work with the param?  
  > **username_1**: There isn't really anything super special about AJAX calls - a GET is a GET and a POST is a POST. If it works with a query string, the issue must be with your JS. Are you using jQuery?  
  > **username_0**: @mmikkel yes I am. No worries at least it's working. I'll look into the JS a bit closer. Thanks for your help.  
  
  ---
  
  ## 0 answers
  
  