GlideAjax - Client

The GlideAjax class enables a client script to call server-side code in a script include.

To use GlideAjax in a client script, follow these general steps.

  1. Create a GlideAjax instance by calling the GlideAjax constructor. As the argument to the constructor, specify the name of the script include class that contains the method you want to call.
  2. Call the addParam method with the sysparm_name parameter and the name of the script-include method you want to call.
  3. (Optional) Call the addParam method one or more times to provide the script-include code with other parameters it needs.
  4. Execute the server-side code by calling getXML().
    Note: getXML() is the preferred method for executing the code, because it is asynchronous and does not hold up the execution of other client code. Another method, getXMLWait(), is also available but is not recommended. Using getXMLWait() ensures the order of execution, but can cause the application to seem unresponsive, significantly degrading the user experience of any application that uses it. getXMLWait() is not available to scoped applications.
var ga = new GlideAjax('HelloWorld'); // HelloWorld is the script include class 
ga.addParam('sysparm_name','helloWorld'); // helloWorld is the script include method 
ga.addParam('sysparm_user_name',"Bob"); // Set parameter sysparm_user_name to 'Bob' 
ga.getXML(HelloWorldParse);  /* Call HelloWorld.helloWorld() with the parameter sysparm_user_name set to 'Bob' 
      and use the callback function HelloWorldParse() to return the result when ready */

// the callback function for returning the result from the server-side code
function HelloWorldParse(response) {  
   var answer = response.responseXML.documentElement.getAttribute("answer"); 
   alert(answer);
}

GlideAjax - addParam(String parm_name, String parm_value)

Specifies a parameter name and value to be passed to the server-side function associated with this GlideAjax object.

You can execute addParam multiple times with different parameters and values.
Note: The first call to addParam should be with the parameter sysparm_name and the name of the server-side method you want to call. The server-side code does not execute until the client script calls getXML().
Table 1. Parameters
Name Type Description
parm_name String The name of the parameter to pass. (The name must begin with the sysparm_ .)
parm_value String The value to assign to parm_name.
Table 2. Returns
TypeDescription
void

GlideAjax - getAnswer()

Retrieves the results from a server-side method called from the client via getXMLWait().

Table 3. Parameters
Name Type Description
none
Table 4. Returns
TypeDescription
voidThe result returned by the server-side method previously called with getXMLWait().

GlideAjax - getXML(Function callback)

Sends the server a request to execute the method and parameters associated with this GlideAjax object.

The server processes the request asynchronously and -- when ready -- returns the results via the function specified as the callback.

Table 5. Parameters
Name Type Description
callback Function The callback function to process the results returned by the server.
Table 6. Returns
TypeDescription
void

GlideAjax - getXMLAnswer(Function callback)

Call the processor asynchronously and get the answer element of the response in XML format.

Table 7. Parameters
Name Type Description
callback Function The callback function. The function receives the answer element of the response in XML format as an argument.
Table 8. Returns
Type Description
void
function updateAttachmentCount(sysid) {
    var ga = new GlideAjax('AttachmentAjax');
    ga.addParam('sysparm_type', 'attachmentCount');
    ga.addParam('sysparm_value', sysid);
    ga.getXMLAnswer(numberOfAttachments, null, sysid); // callback: numberOfAttachments
}

function numberOfAttachments(answer, sysid) {
	// we want to know there are 5 attachments, not 5.0 attachments
	var number = parseInt(answer);
	var buttons = $$('.attachmentNumber_' + sysid);
	if (buttons[0] == undefined)
		$('header_attachment_list_label').down().innerHTML = number;
	else {
		for (var i = 0; i < buttons.length; i++) {
			buttons[i].innerHTML = number;
		}
	}
}

GlideAjax - getXMLWait()

Sends the server a request to execute the method and parameters associated with this GlideAjax object.

The server processes the request synchronously and will not process further requests from the client until finished. To retrieve the results, the client must call getAnswer(). Using getXMLWait() ensures the order of execution, but can cause the application to seem unresponsive, significantly degrading the user experience of any application that uses it. We recommend using getXML() instead.
Note: getXMLWait() is not available to scoped applications.
Table 9. Parameters
Name Type Description
none
Table 10. Returns
TypeDescription
void

Scoped equivalent

getXMLWait() is not available to scoped applications. Instead use the getXML() method.

var ga = new GlideAjax('HelloWorld');
      ga.addParam('sysparm_name','helloWorld');
      ga.addParam('sysparm_user_name',"Bob");
      ga.getXMLWait();
      alert(ga.getAnswer());

GlideAjax - GlideAjax(String class_name)

Constructor for GlideAjax.

Table 11. Parameters
Name Type Description
class_name String The name of the server-side class that contains the method you want to execute.