Reputation: 16802
I'm trying to perform some AJAX validation in a submit() handler for a web form. The ajax is being performed with $.ajax()
from jquery. If I use async: false
it works great but I get a deprecation notice and I'd just assume have code that's future proof.
So my question is how can I perform AJAX validation without async: false
?
https://stackoverflow.com/a/9865124/569976 recommends the when()
function. idk how I can use that for my purposes. I suppose I could have that set a global variable complete
to true
in the done()
part of the when()
method but how do I know when complete
is set to true? I suppose I could do setInterval
to keep on checking the value of complete
until it's finally true and could then use JS to submit the form but idk that seems inelegant.
https://stackoverflow.com/a/14038050/569976 mentions $.ajaxStop
but I gather that's not best practices.
I also tried this (in the $('...').submit(...)
method):
await Promise.all(function() {
return $.ajax(...);
})
That got me a Uncaught SyntaxError: await is only valid in async function
error.
Any ideas?
Upvotes: 0
Views: 87
Reputation: 1631
You should perform your input sanitization and validations before you submit via Ajax. So you could do it before you construct the Ajax request or in a beforeSend event... Preferably as early as possible so you could avoid canceled/aborted constructed Ajax requests.
As for your syntax error, I would check out the documentation on Promise.all as to the type it returns.
Upvotes: 1