Introduction
GlideAjax is a powerful tool in ServiceNow that enables client scripts to communicate asynchronously with server-side Script Includes. This is especially useful when retrieving data from the server without requiring a page refresh.
In this blog, we will explore how to use the getXML()
, getXMLWait()
, and getXMLAnswer()
methods to call server-side functions using GlideAjax.
1. Using getXML()
(Asynchronous – Preferred)
This method sends a request to the server and processes the response using a callback function.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('SampleScriptInclude');
ga.addParam('sysparm_name', 'getServerMessage');
ga.getXML(parseResponse);
function parseResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert("Response from server: " + answer);
}
}
2. Using getXMLWait()
(Synchronous – Not Recommended)
This method waits for the server response, blocking execution, which can impact performance.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('SampleScriptInclude');
ga.addParam('sysparm_name', 'getServerMessage');
var response = ga.getXMLWait();
var answer = response.responseXML.documentElement.getAttribute("answer");
alert("Response from server: " + answer);
}
3. Using getXMLAnswer()
(Asynchronous & Cleaner)
This method returns only the answer
attribute, making response handling simpler and cleaner.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('SampleScriptInclude');
ga.addParam('sysparm_name', 'getServerMessage');
ga.getXMLAnswer(function(answer) {
alert("Response from server: " + answer);
});
}
Server-Side Script Include (Used for All Examples)
The following Script Include processes the request from the client script and returns a response:
var SampleScriptInclude = Class.create();
SampleScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getServerMessage: function() {
return "Hello from Server-Side!";
}
});
Comparison Table
Method | Type | Blocking Execution? | Recommended? | Notes |
---|---|---|---|---|
getXML() | Asynchronous | No | Yes | Uses a callback function to process the XML response. |
getXMLWait() | Synchronous | Yes | No | Blocks execution, which can cause performance issues. |
getXMLAnswer() | Asynchronous | No | Yes | Returns only the answer attribute, simplifying response handling. |
Which One Should You Use?
- Use
getXMLAnswer()
if your Script Include returns a singleanswer
attribute. - Use
getXML()
if you need to process an XML response. - Avoid
getXMLWait()
unless absolutely necessary, as it blocks execution and can degrade performance.
Conclusion
Using GlideAjax, you can efficiently fetch and display data from the server without refreshing the page. The choice of method depends on whether you need XML processing or a direct response.
To enhance performance and improve user experience, always prefer asynchronous methods like getXML()
and getXMLAnswer()
.
Let us know in the comments which method you prefer and why! 🚀