<p>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 <code>{success: true}</code> back, but I want to be able to get <code>{success: true, code: "XXXXX"}</code> after save</p>

<p>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"</p>

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

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

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

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

  });
});
</code></pre>

<p>Thanks </p>

CC BY-SA 4.0
2019-04-30T13:37:09.073Z
Where is the value `code` coming from? Is it part of the data model that is saved?
Ben Parizek
CC BY-SA 4.0
2019-04-30T13:40:30.210Z
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
CC BY-SA 4.0
2019-04-30T13:41:32.233Z
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
CC BY-SA 4.0
2019-04-30T13:43:16.793Z
Question updated
Hassan Mahmood
CC BY-SA 4.0
2019-04-30T16:26:23.417Z
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
CC BY-SA 4.0
2019-05-01T11:50:47.940Z
Because the token is a unique variable which needs to be appended a string to make up a redirect url so something like http://mywebsite.com?token=xxxxx

Is it possible to send this token back in the AJAX response?

I'll updated the question too
Hassan Mahmood
CC BY-SA 4.0
2019-05-01T12:11:20.653Z
Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/93096/discussion-between-hassan-mahmood-and-ben-parizek).
Hassan Mahmood
CC BY-SA 4.0
2019-05-07T12:17:37.687Z
I still don't understand why the token needs to be part of the URL. I don't see a way to comment in the linked chat. Please drop me a note at sprout@barrelstrengthdesign.com if you want to continue the thread elsewhere or ping me on Craft Discord and happy to discuss further.
Ben Parizek
<plugin-development><ajax><plugin-sproutforms>
Sprout plugin: Send information in AJAX success response
# Sprout plugin: Send information in AJAX success response

(Asked by: username_0 on 2019-04-29)

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) {
  event.preventDefault();
  // prepare some variables
  var postUrl = 'sprout-forms/entries/save-entry';
  var formData = $(this).serialize();

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

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

  });
});

Thanks

**Tags:** plugin-development, ajax, plugin-sproutforms

> **Comments:**
> **username_1**: Where is the value `code` coming from? Is it part of the data model that is saved?  
> **username_1**: 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?  
> **username_0**: 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  
> **username_0**: Question updated  
> **username_1**: 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?  
> **username_0**: Because the token is a unique variable which needs to be appended a string to make up a redirect url so something like http://mywebsite.com?token=xxxxx  Is it possible to send this token back in the AJAX response?  I'll updated the question too  
> **username_0**: Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/93096/discussion-between-hassan-mahmood-and-ben-parizek).  
> **username_1**: I still don't understand why the token needs to be part of the URL. I don't see a way to comment in the linked chat. Please drop me a note at sprout@barrelstrengthdesign.com if you want to continue the thread elsewhere or ping me on Craft Discord and happy to discuss further.  

---

## 0 answers

