ajax request inside fd.onsubmit() not working
Hi,
I have an async request inside of an fd.onsubmit() function. Issue is that Mozilla based browsers (e.g. Firefox) are transmitting the form before the ajax response...
here is my example code
Do you have an idea how to handle it?
br, Alex
I have an async request inside of an fd.onsubmit() function. Issue is that Mozilla based browsers (e.g. Firefox) are transmitting the form before the ajax response...
here is my example code
Code: Select all
fd.onsubmit(function (postURL,data) {
jQuery.ajax({
url: postURL,
method: "POST",
headers: {
"Accept": "application/json",
"Content-Type": "application/json"
},
data: JSON.stringify(data),
success: function(){
console.log("ok");
}
});
return true;
});
br, Alex
- Nikita Kurguzov
- Posts: 889
- Joined: Mon Jul 03, 2017
Dear Alex,
Yes, that's what is expected to happen. You can either execute Async requests outside of onsubmit() event, or do it inside, but return false, and then activate fd.save().click() again, after the request is successfully resolved, and this time return true (after checking for the appropriate condition that the request has been executed).
Yes, that's what is expected to happen. You can either execute Async requests outside of onsubmit() event, or do it inside, but return false, and then activate fd.save().click() again, after the request is successfully resolved, and this time return true (after checking for the appropriate condition that the request has been executed).
Cheers
you mean like this ?:
Code: Select all
fd.onsubmit(function (postURL,data) {
jQuery.ajax({
url: postURL,
method: "POST",
headers: {
"Accept": "application/json",
"Content-Type": "application/json"
},
data: JSON.stringify(data),
success: function(){
console.log("ok");
fd.save().click();
}
});
return false;
});
- Nikita Kurguzov
- Posts: 889
- Joined: Mon Jul 03, 2017
Dear Alex,
Yes, try it like this:
This should work, but if it doesn't - let me know!
Yes, try it like this:
Code: Select all
var success = false;
fd.onsubmit(function (postURL,data) {
jQuery.ajax({
url: postURL,
method: "POST",
headers: {
"Accept": "application/json",
"Content-Type": "application/json"
},
data: JSON.stringify(data),
success: function(){
console.log("ok");
success = true;
fd.save().click();
}
});
if (success)
return true;
return false;
});
Cheers
I think, I got it with
br, Alex
Code: Select all
var success = false;
fd.onsubmit(function (postURL,data) {
if(!success){
jQuery.ajax({
url: postURL,
method: "POST",
headers: {
"Accept": "application/json",
"Content-Type": "application/json"
},
data: JSON.stringify(data),
success: function(){
console.log("ok");
success = true;
fd.save().click();
}
});
}
if (success)
return true;
return false;
});
-
- Information
-
Who is online
Users browsing this forum: No registered users and 17 guests