How to load content in a div with ajax? After validate with javascript that I'll be able to use the client location:

if (navigator.geolocation) {
...


I need to load the content of a view block with arguments (based in the position coords). I know how to obtain the HTML that I want to put inside the div, so at least I have this part.

I tried to follow the Simple Drupal AJAX load with jQuery and delivery callback article (that is write for Drupal 7 very different from Drupal 8) but I just arrived to load a page in a div (with headers and footer very far from my real objetive).

jQuery('#my-div').load('/admin/help');


Any ideas on how to load content in a div with ajax?
I've come up with a working solution. It might not be the best approach but it works.

You need to create your routing file:

my_module.routing.yml

diocese_core.path_with_data:
  path: 'path-with-data'
  defaults:
    _controller: '\Drupal\my_module\Controller\MyController::Render'
  requirements:
    _access: 'TRUE'


Creating the controller for your page

src/Controller/MyController.php

&lt;?php

namespace Drupal\my_module\Controller;

use Drupal\Core\Controller\ControllerBase;
use \Symfony\Component\HttpFoundation\Response;

/**
 * An example controller.
 */
class MyController extends ControllerBase {

  /**
   * {@inheritdoc}
   */
  public function Render() {
    $build = array(
      '#type' =&gt; 'markup',
      '#markup' =&gt; t('Hello World!'),
    );
    // This is the important part, because will render only the TWIG template.
    return new Response(render($build));
  }

}


And you only need to call in javascript

jQuery('#my-div').load('path-with-data');


An this will be the output once the code is executed:

&lt;div id="my-div"&gt;Hello World!&lt;/div&gt;


References:


Drupal 8 module - How to use AJAX?
Drupal8:Basic Jquery Ajax custom module controller