How do I add drupalSettings info from an AJAX callback I have a custom Drupal 7 module that does a zipcode lookup to return town and county. I set Drupal.settings.zip_code_success to true within the callback. This is the code I am using.

drupal_add_js(array('zip_code_success' =&gt; TRUE), 'setting');
$commands = array();
$commands[] = ajax_command_html('#triage_city', $out);
return array(
  '#type' =&gt; 'ajax',
  '#commands' =&gt; $commands,
);


The zip_code_success value is then available in some processing code that runs on the js side.

In moving things to Drupal 8, the lookup and html replacement are working fine, but I can't figure out how to get the zip_code_success info into drupalSettings.  drupalSettings gets set up in the the initial page generation and shows within the DOM with the values that are passed initially through the hook_preprocess_html function.  I've tried a variety of things in the zipcode lookup function but haven't been able to update drupalSettings.  Here's the current D8 code snippet comparable to the D7 above:

$response-&gt;addCommand(new SettingsCommand(['zip_code_success'=&gt;TRUE] ,TRUE));
$response-&gt;addCommand(new HtmlCommand('#triage_city', $out));
return $response;


I've also tried:

$response-&gt;setAttachments([
  'library' =&gt; ['core/drupalSettings'],
  'drupalSettings' =&gt; ['zip_code_success' =&gt; TRUE]
]);
$response-&gt;addCommand(new HtmlCommand('#triage_city', $out));
return $response;


Any wisdom as to how to handle this situation?
In Drupal 8, as you've discovered, drupal_add_js() has been removed.

There are two things you need to do this:

1) You cannot add JS in an ajax callback. It was a bad practices in D7, and cannot be done at all in D8. You'll need to add the JS in your form definition, not the ajax callback.

2) You add JS through a two-step process. The first is to create a new library in the *.libraries.yml file. Add a dependency to core/drupalSettings`. Then, in your form definition, you add your setting like this:

$form['some_element'] = [
  '#markup' =&gt; '', // required to render the element
  '#attached' =&gt; [
    'libraries' =&gt; [
      'some_library', // The library you created
    ],
    'drupalSettings' =&gt; [
      'modulename' =&gt; [
        'zip_code_success' =&gt; TRUE,
      ],
    ],
  ],
];