1. getXML()
- Type: Asynchronous.
- Execution: This method sends an AJAX request to the server and does not wait for the server's response before proceeding with the rest of the code. When the server returns the response, a callback function is triggered to handle the result.
- Use Case: Use this when you want the script to keep running without waiting for the server's response, allowing the UI to remain responsive while the server-side work is being processed.
var ga = new GlideAjax('YourScriptInclude'); ga.addParam('sysparm_name', 'yourFunction'); ga.getXML(answerFunction); // Asynchronous function answerFunction(response) { var answer = response.responseXML.documentElement.getAttribute("answer"); gs.info('Server returned: ' + answer); }
- Explanation: After calling
getXML()
, the functionanswerFunction
will be called when the response is received. In the meantime, other operations can continue.
2. getXMLWait()
- Type: Synchronous.
- Execution: This method sends an AJAX request to the server and waits for the server to respond before continuing with the rest of the code. The script execution pauses until the response is received.
- Use Case: Use this when you need to wait for the server's response before proceeding. However, using
getXMLWait()
can block the UI and negatively affect the user experience, so it should be used sparingly.
Example:
var ga = new GlideAjax('YourScriptInclude'); ga.addParam('sysparm_name', 'yourFunction'); var response = ga.getXMLWait(); // Synchronous var answer = response.responseXML.documentElement.getAttribute("answer"); gs.info('Server returned: ' + answer);
- Explanation: In this case, the script waits for the response from the server before proceeding. It doesn't allow the script to continue until the server returns the result.
Key Differences:
Feature
getXML()
(Asynchronous)
getXMLWait()
(Synchronous)
Execution
Sends a request and does not wait for the response. The callback function handles the result.
Sends a request and waits for the response before continuing.
Impact on UI
Keeps the UI responsive as the script doesn't pause for the response.
Can block the UI and freeze the browser until the response is received.
Use Case
Ideal when the server response is not immediately required, and the UI should not be blocked.
Use when the response is critical and the next steps depend on it. Should be used with caution.
Performance
Generally better for user experience and performance, especially in client-side code.
Can degrade performance and affect user experience, especially if the server response takes time.
Example Scenario
Fetching data in the background, like loading additional details without interrupting the user.
Validating data where the response is needed immediately before proceeding (though this is rarely recommended in the browser).
When to Use:
getXML()
: Best for scenarios where you want to allow the user interface to continue being responsive while the server is processing the request. Use it for background tasks where results can be handled after the request completes.getXMLWait()
: Should be used only when absolutely necessary and the script cannot proceed without the server response. However, use it cautiously because it can block the UI and create a poor user experience.