controller function use with ajax I have one controller like as below .

&lt;?php
namespace Drupal\users\Controller;

use Drupal\Core\Controller\ControllerBase;


class UsersController extends ControllerBase {


  public function getList() {
    $time = time();
    $data = array();
    $response = $data = $result = null;

      // add all the data in one multiple dim. array
      $data['title'] = 'Example to consume REST API of User List';
      $data['users'] = $result-&gt;list;
      $data['form_data'] = $result-&gt;form_data;

      // display the content in the middle section of the page
      $build = array(
        '#theme' =&gt; 'users_list', // assign the theme [users-list.html.twig]
        '#title' =&gt; 'Example to consume REST API', 
        '#pagehtml' =&gt; 'API URL : api/users/list ',
        '#data' =&gt; $data
      ); 
    }


    return $build;
  }
}


this above function is called and render the HTML in the middle of the page in HTML format where page URL is call as per defined in routing file (http://siteurl.com/user-list).
I want to use this HTML output in ajax request which will call the same function through ajax instead of page url.

&lt;?php
namespace Drupal\ajax\Controller;

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

class AjaxController extends ControllerBase {

  public function call_ajax(){

      //  my target is like as below
       $op = Drupal\Core\Controller\UsersController()-&gt;getList();
       return $op;
  }
}


Q) Is there any way to get the output of getList() in HTML format in ajax callback?
After lots of searching finally, I got the solution.
I would like to share this.

AjaxController.php

namespace Drupal\ajax\Controller;

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

class AjaxController extends ControllerBase {

  public function call_ajax($controller,$function){
    $args = func_get_args();
    $r = array();
    $r['html'] = null;

    $class = "\\Drupal\\".$controller."\\Controller\\".ucfirst($controller)."Controller";

    $obj = new $class();

    if(method_exists($obj,$function)){
      $r['html'] = render($obj-&gt;$function());
    }
    return new JsonResponse($r);
  }

}


ajax.routing.yml

ajax.call:
  path: '/call/ajax/{controller}/{function}'
  defaults:
    _controller: '\Drupal\ajax\Controller\AjaxController::call_ajax'
    _title: 'Get List'
  methods:  [GET]
  requirements:
    _permission: 'access content'


ajax.js

jQuery(document).ready(function($a){
    $a.ajax({
        url: "call/ajax/users/getList", 
        method :'GET',
        dataType: "json", 
        success: function(result){
          $a(".region-content").html(result.html);
        }
    });
})


Now I have to just pass the controller and function name to call the function using ajax.

The main purpose to use this way is reusability of controller functions.