Loading block via ajax I have an custom module with Block:
namespace Drupal\generator\Plugin\Block;

use Drupal\Core\Block\BlockBase;

class GeneratorBlock extends BlockBase {
    
    public function build() {
        $form = \Drupal::formBuilder()-&gt;getForm('Drupal\generator\Form\ProjektForm');
        
        return array(
          'form' =&gt; $form,
          '#attached' =&gt; array(
            'library' =&gt;  array(
              'generator/generator-css'
            ),
          ),
        );

    }
} 

All it does is placing some From in there with additional .css file (from library). I can place this block via Admin -&gt; Structure -&gt; Block layout and it works fine.
What I want now is to load that block via Ajax (because I want to refresh the whole block in some cases). And by that I mean I want to load it into wrapper (like with $.load(...)) or get the content of the block in JS variable (not the whole page with block, but just the rendered block).
I've found the tutorial for Drupal 7 on drupal.org, but I failed to port it to Drupal 8.
You can use the #ajax attribute to load main part of the block via Drupal ajax.

namespace Drupal\generator\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\generator\Form\ProjektForm;

class GeneratorBlock extends BlockBase {

  public function build() {
    return array(
      'form_wrapper' =&gt; array(
        '#prefix' =&gt; '&lt;div id="form-wrapper"&gt;',
        '#suffix' =&gt; '&lt;/div&gt;',
        '#markup' =&gt; '',
      ),
      'load_form' =&gt; array(
        '#type' =&gt; 'button',
        '#value' =&gt; $this-&gt;t('Load form'),
        '#ajax' =&gt; array(
          'wrapper' =&gt; 'form-wrapper',
          'callback' =&gt; array(static::class, 'getForm'),
        ),
      ),
      '#attached' =&gt; array(
        'library' =&gt;  array(
          'generator/generator-css'
        ),
      ),
    );
  }

  public static function getForm() {
    return \Drupal::formBuilder()-&gt;getForm(ProjektForm::class);
  }

}