I'm using Sprout for registering to events, I want to be able to send a code in the AJAX response when EVENT_AFTER_SAVE occurs. At the moment, I get {success: true} back, but I want to be able to get {success: true, code: "XXXXX"} after save

The code will be coming from a Channel called Tokens, which consists of one field: tokenCode. I want to grab one at random and send tokenCode as "code"

This is what I want my AJAX request to be, note var redirect = '' + data.token

$('#token-form').submit(function(event) {
  // prepare some variables
  var postUrl = 'sprout-forms/entries/save-entry';
  var formData = $(this).serialize();

// process the form
    type : 'POST',
    url : postUrl,
    data : formData,
    dataType : 'json',
    encode : true
  .done(function(data) {
    if (data.success == true)

      var redirect = '' + data.token
      $(location).attr('href', redirect);



  • Where is the value code coming from? Is it part of the data model that is saved? – Ben Parizek Apr 30 '19 at 13:37
  • I see in a separate comment you've said that the code is coming from a Craft Entry. When and where does the code get created? Can you elaborate on the full workflow in your question? – Ben Parizek Apr 30 '19 at 13:40
  • No, it's be coming from an Entry, theres a channel call "tokens", I'll be wanting to get one at random and then pass a field to code – Hassan Mahmood Apr 30 '19 at 13:41
  • Question updated – Hassan Mahmood Apr 30 '19 at 13:43
  • Why do you need this in the AJAX response? Can you just grab a random token on the page when you load the page or thank you page? – Ben Parizek Apr 30 '19 at 16:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.