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.


  • 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
  • @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 Oct 17 '15 at 8:19
  • 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 Oct 17 '15 at 8:24
  • @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 Oct 17 '15 at 8:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.