ajax call breaking onBeforeSaveUser I have a plugin that has an onBeforeSaveUser event. This fires the mailchimpsubscribe plugin to subscribe users to selected newsletters.

Everything works correctly the user is subscribed and then the saveUser function saves the users newsletter preferences to the database.

I am now trying to get it to work with an ajax call, which again works correctly and subscribes the user to mailchimp. However after sending the json response it does not appear that the saveUser function is being fired as nothing is updated in the database for the selections made.

Could the ajax be stopping the saveUser from being fired?

Ajax:

// Get the post data
var data = $(this).serialize();

// Send it to the server
$.post('/', data, function (response) {

    if (response.success) {
      $form.parents('.question-box').fadeOut(function () {
        $(this).remove();
        $('.question-box').first().fadeIn();
      });
    } else {
        var message = response.message;
      alert('Unfortunately, we were unable to save your data at this point. Please try again later.'+message);
    }
});


Mailchimp Plugin:

if ($api-&gt;errorCode) { // an api error occured 

      // Respond appropriately to Ajax Requests.....

} else { // list subscribe was successful

      // Respond appropriately to Ajax Requests
      if (craft()-&gt;request-&gt;isAjaxRequest())
      {
        return $this-&gt;returnJson(array(
          'success' =&gt; true,
          'errorCode' =&gt; 1,
          'values' =&gt; array(
            'email' =&gt; $email,
            'vars' =&gt; $vars
          )
        ));
      }

      //..... 
}


Cheers
Had a response from Pixel &amp; Tonic on this. Which explains the process.



The problem you're seeing is caused by returnJson() as that also ends the process after outputting the JSON data. Most likely onSaveUser (without Before) is a better place for this code; that ensures the user has been saved to the database before running the event code.