Is it possible to get ajax params in json?

I’m trying to implement “Display more pages” button using jQuery ajax and twig template.

My javascript looks like
url: “/products.json.twig”,
method: “GET”,
data: { index: “0” }, //page index to show
success: function (data) {

Is it possible to read somehow data parameter in products.json.twig using twig? If not, how should I solve this problem? Thanks in advance!

I think you are looking for this ?

You also have access to the Uri object in Twig so you should be able to do this:

{% set var = uri.param('foo') %}

That AJAX request builds a request of type: /products?index=0, so you need to use uri.query('index') to get the value. uri.param() is to be used for grav parameters, which are more readable, e.g., but the jQuery Ajax call does not builds urls in this style.

Take a look at the tests for some pretty comprehensive examples of various URL structures.

Also, don’t point your request to products.json.twig. Point it to products, if that is the URL that lists the products, and ask the server to return JSON, or use $.getJSON instead.