How to do a simple ajax request


(Jürgen Kleiß) #1

Do your typical ajax request with your javascript library you like. For example with jquery:

In this example we call the url with some get parameters and append the returned html to some HTML element.

your-domain.com/?ajax=true&nodeId=some-node-id

$.get("/", {ajax: true, nodeId: 'some-node-id') .done(function( html ) {
    $('#some-html-element).append(html);
});

Now we have to do some TypoScript work.

Catch the ajax requests.
We create a new array element root.ajax which gets activated if the get parameter “ajax” which you get out of request.arguments.ajax is true.

root.ajax {
    condition = ${request.arguments.ajax == 'true' ? true : false}
type = 'YourCompany.YourPackage:AjaxType'
    position = '10'
}

This prototype renders all the child elements of a specific node with the given nodeId

prototype(YourCompany.YourPackage:AjaxType) < prototype(TYPO3.TypoScript:Collection) {
    collection = ${q(node).find('#' + request.arguments.nodeId).children()}
    itemRenderer = TYPO3.Neos:ContentCase
    itemName = 'node'
}

You also have to set a new cache entry identifier

root.@cache.entryIdentifier.projects = ${'ajax' + request.arguments.nodeId}

I got some help on the neos slack channel to create that code so i was requested to share that with the community. If there is an error or something should be done in an other way please let me know :slight_smile: