title: How to obtain response from form submitted by AJAX?
body: <p>I want to use Acymailing Joomla! component installed at <strong>example.com/mailer</strong> to manage <strong>subscriptions from non Joomla site on example.com</strong></p>
  
  <p>In that case I have simple script</p>
  
  <pre><code> $(function () {
      $('form').on('submit', function (e) {
        e.preventDefault();
        $.ajax({
          type: 'post',
          url: 'https://example.com/mailer/index.php?option=com_acymailing&amp;ctrl=sub',
          data: $('form').serialize(),
          success: function () {
            swal('Great success!');
          }
        });
      });
    });
  </code></pre>
  
  <p>and form</p>
  
  <pre><code>&lt;form class="form-inline" action="https://example.com/mailer/index.php?option=com_acymailing&amp;ctrl=sub" method="post"&gt;
      &lt;div class="form-group"&gt;
          &lt;label class="sr-only" for="user_name"&gt;Email address&lt;/label&gt;
          &lt;input id="user_name"   type="text" name="user[name]" value="" class="form-control" placeholder="Email"&gt;
      &lt;/div&gt;
      &lt;div class="form-group"&gt;
          &lt;label class="sr-only" for="user_email"&gt;Password&lt;/label&gt;
          &lt;input id="user_email"   type="text" name="user[email]" value="" class="form-control" placeholder="Password"&gt;
      &lt;/div&gt;
      &lt;button type="submit" class="btn btn-default"&gt;Sign Up!&lt;/button&gt;
      &lt;input type="hidden" name="user[html]" value="1" /&gt;
      &lt;input type="hidden" name="acyformname" value="formAcymailing1" /&gt;
      &lt;input type="hidden" name="ctrl" value="sub"/&gt;
      &lt;input type="hidden" name="task" value="optin"/&gt;
      &lt;input type="hidden" name="redirect" value="https://example.com"/&gt;
      &lt;input type="hidden" name="option" value="com_acymailing"/&gt;
      &lt;input type="hidden" name="visiblelists" value=""/&gt;
      &lt;input type="hidden" name="hiddenlists" value="1"/&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Everything works fine except success, error states...</p>
  
  <p>Joomla Acymailing have sub.php file to handle ajax responses</p>
  
  <pre><code> if($config-&gt;get('subscription_message',1) || $ajax){
          if($allowSubscriptionModifications){
              if($statusAdd == 2){
                  if($userClass-&gt;confirmationSentSuccess){
                      $msg = 'CONFIRMATION_SENT';
                      $code = 2;
                      $msgtype = 'success';
                  }else{
                      $msg = $userClass-&gt;confirmationSentError;
                      $code = 7;
                      $msgtype = 'error';
                  }
              }else{
                  if($insertMessage){
                      $msg = 'SUBSCRIPTION_OK';
                      $code = 3;
                      $msgtype = 'success';
                  }elseif($updateMessage){
  
                      $msg = 'SUBSCRIPTION_UPDATED_OK';
                      $code = 4;
                      $msgtype = 'success';
                  }else{
                      $msg = 'ALREADY_SUBSCRIBED';
                      $code = 5;
                      $msgtype = 'success';
                  }
              }
          }else{
              if($modifySubscriptionSuccess){
                  $msg = 'IDENTIFICATION_SENT';
                  $code = 6;
                  $msgtype = 'warning';
              }else{
                  $msg = $modifySubscriptionError;
                  $code = 8;
                  $msgtype = 'error';
              }
          }
  
          if($msg == strtoupper($msg)){
              $source = acymailing_getVar('cmd', 'acy_source');
              if(strpos($source, 'module_') !== false){
                  $moduleId = '_'.strtoupper($source);
                  if(acymailing_translation($msg.$moduleId) != $msg.$moduleId) $msg = $msg.$moduleId;
              }
              $msg = acymailing_translation($msg);
          }
  
          $replace = array();
          $replace['{list:name}'] = '';
          foreach($myuser as $oneProp =&gt; $oneVal){
              $replace['{user:'.$oneProp.'}'] = $oneVal;
          }
          $msg = str_replace(array_keys($replace),$replace,$msg);
  
          if($config-&gt;get('redirect_tags', 0) == 1) $redirectUrl = str_replace(array_keys($replace),$replace,$redirectUrl);
  
          if($ajax){
              $msg = str_replace(array("\n","\r",'"','\\'),array(' ',' ',"'",'\\\\'),$msg);
              echo '{"message":"'.$msg.'","type":"'.($msgtype == 'warning' ? 'success' : $msgtype).'","code":"'.$code.'"}';
          }elseif(empty($redirectUrl)){
              acymailing_enqueueMessage($msg,$msgtype == 'success' ? 'info' : $msgtype);
          }else{
              if(strlen($msg)&gt;0){
                  if($msgtype == 'success') acymailing_enqueueMessage($msg);
                  elseif($msgtype == 'warning') acymailing_enqueueMessage($msg,'notice');
                  else acymailing_enqueueMessage($msg,'error');
              }
          }
      }
  </code></pre>
  
  <p>And JSON looks like on Joomla side registration to the same form by index.php?option=com_acymailing&amp;ctrl=sub</p>
  
  <pre><code> message    Subscribe confirmed
   type   success
   code   3
  
   {"message":"Subscribe confirmed","type":"success","code":"3"}
  </code></pre>
  
  <p>The question is: how to obtain that submission statuses success, error, already submbited etc on external submission form (<strong>at example.com page</strong>)?</p>