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
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 Oct 16 '15 at 20:25