The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Client-side code should not use synchronous AJAX ; Really ?

Fred Jean
Tera Expert

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 ?

16 REPLIES 16

I know.

Note that is post is not about getting technical advice on how to do something.

It's about debating the fact that synchronous AJAX is a bad practice, while it seems that most of the time when we have an AJAX call on onChange script that would be to populate some other fields and means we then would expect the form to be blocked until this is done to avoid submission with incomplete or inconsistant data. And thus, even if we have (more complex) alternatives to block submission, synchronous calls might not be that bad or that asynchronous would not necessarily be the preferred option.

Or that at least, without implementing a mechanism to block submission, synchronous call is better for data integrity.

@Fred Jean 

You can always raise an idea on idea portal and ServiceNow can have a look on it.

ServiceNow Idea Portal on Now Support 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader