CMS Development

skhirouni
Participant

Redirection after form submission is terminating my ajax call

SOLVE

I am sending an ajax request  to my server after form submission ,I set the call of the ajax request in the call back function of onFormSubmit.The form i am working on has a external redirection link once submitted , the problem is I am seeing my ajax request aborted , I am suspecting that it's due to the fast redirection so my question is there is a way to delay it ?

I can always work with redirecting manually but i prefer not to for maintenance purposes (none dev can change easily the redirection links) 

Thanks in advance 

0 Upvotes
1 Accepted solution
tjoyce
Solution
Recognized Expert | Elite Partner
Recognized Expert | Elite Partner

Redirection after form submission is terminating my ajax call

SOLVE

@skhirouni - There is no way to delay it if you set it like that... you would have to put the redirect in your ajax callback complete function with 

$.ajax({
  url: "test.html",
  context: document.body
}).done(function() {
  window.location.href = "https://myredirecturl"
});

View solution in original post

3 Replies 3
tjoyce
Solution
Recognized Expert | Elite Partner
Recognized Expert | Elite Partner

Redirection after form submission is terminating my ajax call

SOLVE

@skhirouni - There is no way to delay it if you set it like that... you would have to put the redirect in your ajax callback complete function with 

$.ajax({
  url: "test.html",
  context: document.body
}).done(function() {
  window.location.href = "https://myredirecturl"
});
dennisedson
HubSpot Product Team
HubSpot Product Team

Redirection after form submission is terminating my ajax call

SOLVE

Heyo @skhirouni 

Mind adding a code block here so we can see what you are up to?

@tjoyce , @Kevin-C might either of you have words of advice 😀

0 Upvotes
skhirouni
Participant

Redirection after form submission is terminating my ajax call

SOLVE

Hello thank you for the quick response here is a code sample of a javascript file that i am injecting in the header of the page and I am setting an external link redirection once the form submitted

/** function doSaveLeadInCookiesAndRequestItToIntercom() {
var url ="my_url"

$.ajax({
type: "POST",
url: url,
data : JSON.stringify(data),
contentType : "application/json"
})
.done(function( data ) {
Cookies.set('lead', JSON.stringify(data.ResultItem) );
});
}

window.addEventListener('message', function(event) {
if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmit') {
getCustomerCaseFormData(); //getting the form data using jquery
doSaveLeadInCookiesAndRequestItToIntercom();// this will send an ajax call 
}
});

window.addEventListener('message', function(event) {
if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmitted') {
//doSaveLeadInCookiesAndRequestItToIntercom();
}
}); /**

the problem is my ajax call is terminated and I am suspecting the redirection is responsable for that , so is there a way to make the redirection waits the completion of my ajax call . Ps : I am using this redirection.

skhirouni_0-1620485550846.png

0 Upvotes