I'm currently trying to use AJAX (with jQuery) in my Drupal 8 module in order to reload a page when the user types something in a textfield. I managed to get a full HTML page back but I would like the AJAX function to only get the content of the page (without the whole layout).
How can I achieve that? What shall I change in my controller function in order to render a different view when the request uses AJAX?
I have searched on Google and on the Drupal 8 documentation but I have not found any answer to my question.
Thanks in advance for your help!
You can return the data in AJAX callback function as below.
$build[] = array(
'#type' => 'markup',
'#markup' => '<p>Demo text content</p>',
);
return new Response(render($build));
Make sure to include the Response class
use \Symfony\Component\HttpFoundation\Response;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With