Client-side code should not use synchronous AJAX ; Really ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
8 hours ago - last edited 7 hours ago
Hello,
It is a well known, and I believe accepted, good practice to avoid synchronous AJAX in client script. This is for reasons such as UI responsiveness and perf bottleneck. And having such synchronous AJAX will trigger Healthscan findings.
While reviewing such Healthscan findings where a catalog client script was using a synchronous call, I discussed the topic with the script developer to know if there was any reason to do so.
The reason I was given was to avoid the user from submitting the form before we get the Ajax answer to update other form fields. (yes, the user would have to be very quick or the network very slow, but it can happen). So here we want to avoid submission of incomplete or inconsistant data.
So then I thought, doesn't this actually also apply to like 90% of the client scripts using AJAX calls ? Don't we mostly use AJAX to update other fields ? Isn't it an issue most of the time if a form is submitted before the script and AJAX does its job ? So, isn't it actually a good thing to have synchronous calls and get the form blocked (yes I know there are other ways to achieve this) ?
What do you think ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
7 hours ago
yes sometimes it's require to stop form submission using onSubmit validation and there you need Synchronous GlideAjax.
But you can always move the validation from onSubmit to onChange of variable and throw error and form won't be submitted.
Synchronous GlideAjax don't work in portal.
Refer these links for workaround/solution on how to use Synchronous GlideAjax in onSubmit
How To: Async GlideAjax in an onSubmit script
Asynchronous onSubmit Catalog/Client Scripts in ServiceNow
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
7 hours ago - last edited 6 hours ago
You say "sometimes", but my point is that it might actually "most of the time". I mean when would we actually be happy that a form gets submitted before having the other fields be set by the AJAX call ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
7 hours ago
onSubmit with Synchronous is mostly used to validate if user is allowed to submit form or not.
Well not all customers prefer onSubmit validation and giving a bad UI experience to let them fill everything and then stop the submission.
I have seen some customers preferring onSubmit due to some requirement but most of the times I have recommended onChange validation on variable or field to handle this.
Now that the platform doesn't allow you have other ways to achieve that if it's still required.
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
6 hours ago
Agree it's better to have validation onChange, which is why I would think that most the time, we want the form to be blocked until that on change validation (including AJAX call) is done.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 hours ago
you can achieve this with onSubmit + Synchronous using the links I shared above.
sharing again here
How To: Async GlideAjax in an onSubmit script
Asynchronous onSubmit Catalog/Client Scripts in ServiceNow
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader