<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>

CC BY-SA 3.0
2015-10-16T20:25:59.693Z
`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`)?
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2015-10-17T08:19:08.993Z
@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?
Lettie
CC BY-SA 3.0
2015-10-17T08:24:01.233Z
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?
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2015-10-17T08:44:01.727Z
@mmikkel yes I am. No worries at least it's working. I'll look into the JS a bit closer. Thanks for your help.
Lettie
<templating><ajax><params>
How to get a param from an ajax request in twig?
# 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

