{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":654,"path":"api-reference/scripts/p_AJAX.md","title":null,"bundle":null,"doc_type":null,"release":null,"body":"---\ntitle: AJAX\ndescription: AJAX \\(asynchronous JavaScript and XML\\) is a group of interrelated, client-side development techniques used to create asynchronous Web applications.The GlideAjax class allows the execution of server-side code from the client. GlideAjax calls pass parameters to the script includes, and, using naming conventions, allows the use of these parameters.There are two parts to the asynchronous GlideAjax script: client-side and server-side code.Use synchronous when your script cannot continue without the GlideAjax response. This stops the session until the response is received.Provides helper functions for Ajax clients to retrieve a value from an Ajax client.\nlocale: en-US\ncanonical_url: https://www.servicenow.com/docs/r/australia/api-reference/scripts/p\\_AJAX.html\nrelease: australia\nproduct: Scripts\nclassification: scripts\ntopic_type: topic\nlast_updated: \"2026-03-12\"\nreading_time_minutes: 5\nbreadcrumb: [Client-side scripting, Scripting, API implementation, API implementation and reference]\n---\n\n# AJAX\n\nAJAX \\(asynchronous JavaScript and XML\\) is a group of interrelated, client-side development techniques used to create asynchronous Web applications.\n\nAJAX enables web applications to send and retrieve information to and from a server in the background, without impacting the user experience with the displayed web page.\n\n**Parent Topic:**[Client-side scripting](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/scripts/client-side-scripting-overview.md)\n\n## GlideAjax\n\nThe GlideAjax class allows the execution of server-side code from the client. GlideAjax calls pass parameters to the script includes, and, using naming conventions, allows the use of these parameters.\n\n**Note:** This functionality requires a knowledge of JavaScript.\n\nUsing GlideAjax:\n\n-   Initialize GlideAjax with the name of the script include that you want to use.\n-   When creating the script include, you must set the name field to be exactly the same as the class name.\n-   When creating the script include, you must select the **Client callable** check box.\n-   Specify the parameter **sysparm\\_name**. GlideAjax uses**sysparm\\_name** to find which function to use.\n-   Any extra parameters may be passed in, all of which must begin with **sysparm\\_**. Avoid using predefined parameter names:\n    -   **sysparm\\_name**\n    -   **sysparm\\_function**\n    -   **sysparm\\_value**\n    -   **sysparm\\_type**\n-   Code is then executed with the getXML\\(\\) or getXMLWait\\(\\) functions.\n\nFor additional information, refer to the [GlideAjax](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/c_GlideAjaxAPI.md) API.\n\n## Examples of asynchronous GlideAjax\n\nThere are two parts to the asynchronous GlideAjax script: client-side and server-side code.\n\n### Hello World: Returning a value from the server\n\n### Client side\n\nThis code runs on the client \\(the web browser\\). Create a client script as normal. This sends the parameters to server, which then does the processing. So that the client does not wait for the result, a callback function is used to return the result, passed to the getXML\\(\\) function. \\(In this case it is called `HelloWorldParse`.\\)\n\nThe getXMLWait\\(\\) function does not need a separate callback function, but this will block the client. If the client-server communication takes a long time \\(for example on slow networks\\), the application will seem unresponsive and slow. An example of getXMLWait\\(\\) is in the following section.\n\n```\nvar ga = new GlideAjax('HelloWorld');\nga.addParam('sysparm_name', 'helloWorld');\nga.addParam('sysparm_user_name', \"Bob\");\nga.getXML(HelloWorldParse);\n \nfunction HelloWorldParse(response) {\n  var answer = response.responseXML.documentElement.getAttribute(\"answer\");\n  alert(answer); }\n```\n\n### Server side\n\nThe server-side code for the above function. Do not create a business rule, but instead navigate to **System Definition &gt; Script Include** and create a new script. Paste in the code below.\n\n**Note:** You must set the name of the script include to `HelloWorld`.\n\n-   The sys\\_script\\_include code must extend the AbstractAjaxProcessor class and be Glide AJAX enabled.\n-   Function names starting with \"\\_\" are considered private and are not callable from the client.\n-   Avoid overriding methods of AbstractAjaxProcessor, including `initialize`. While it is possible to invoke methods of your superclass object which you have overridden, it is complicated and best avoided altogether.\n\n```\nvar HelloWorld = Class.create();\nHelloWorld.prototype = Object.extendsObject(AbstractAjaxProcessor, {\n   helloWorld:function() { return \"Hello \" + this.getParameter('sysparm_user_name') + \"!\"; } ,\n   _privateFunction: function() { // this function is not client callable     \n    }\n });\n```\n\nThis results in an alert box that says 'Hello Bob!' when you visit the form.\n\n### Returning multiple values\n\nSince the response is an XML document we are not limited to returning a single `answer` value. Here is a more complex example returning multiple XML nodes and attributes.\n\n### AJAX processor script include\n\n```\n/*\n * MyFavoritesAjax script include Description - sample AJAX processor returning multiple value pairs\n */ \nvar MyFavoritesAjax = Class.create();\nMyFavoritesAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {\n \n\t /*\n\t * method available to client scripts call using:\n\t * var gajax = new GlideAjax(\"MyFavoritesAjax\");\n\t * gajax.addParam(\"sysparm_name\",\"getFavorites\");\n\t */\n\tgetFavorites: function() { // build new response xml element for result\n         var result = this.newItem(\"result\");\n\t  result.setAttribute(\"message\",\"returning all favorites\");\n \n\t  //add some favorite nodes with name and value attributes \n         this._addFavorite(\"color\",\"blue\");\n         this._addFavorite(\"beer\",\"lager\");\n         this._addFavorite(\"pet\",\"dog\");\n       },\n\t // all items are returned to the client through the inherited methods of AbstractAjaxProcessor\n\t _addFavorite: function(name, value) { \n          var favs = this.newItem(\"favorite\");\n\t   favs.setAttribute(\"name\",name);\n\t   favs.setAttribute(\"value\",value); },\n \n\ttype:\"MyFavoritesAjax\"\n \n });\n```\n\n### Client script\n\n```\n// new GlideAjax object referencing name of AJAX script include \nvar ga = new GlideAjax(\"MyFavoritesAjax\"); \n// add name parameter to define which function we want to call \n// method name in script include will be getFavorites\nga.addParam(\"sysparm_name\",\"getFavorites\");\n \n// submit request to server, call ajaxResponse function with server response\n \nga.getXML(ajaxResponse);\n \nfunction ajaxResponse(serverResponse) { \n  // get result element and attributes\n  var result = serverResponse.responseXML.getElementsByTagName(\"result\");\n  var message = result[0].getAttribute(\"message\");\n \n  //check for message attribute and alert user\n  if(message) alert(message);\n \n  //build output to display on client for testing \n  var output = \"\";\n \n  // get favorite elements \n  var favorites = serverResponse.responseXML.getElementsByTagName(\"favorite\");\n  for(var i = 0; i < favorites.length; i ++) { \n    var name = favorites[i].getAttribute(\"name\");\n    var value = favorites[i].getAttribute(\"value\");\n    output += name + \" = \" + value + \"\\n \"; }\n \n  alert(output); }\n```\n\n### XML response\n\n```\n<xml sysparm_max= \"15\" sysparm_name=\"getFavorites\" sysparm_processor=\"MyFavoritesAjax\">\n  <result message = \"returning all favorites\"></result>\n  <favorite name = \"color\" value = \"blue\"></favorite>\n  <favorite name = \"beer\" value = \"lager\"></favorite>\n  <favorite name = \"pet\" value = \"dog\"></favorite>\n</xml>\n```\n\n## Examples of synchronous GlideAjax\n\nUse synchronous when your script cannot continue without the GlideAjax response. This stops the session until the response is received.\n\nIf your use case demands that no further processing can occur until the GlideAjax response has been received, you can use getXMLWait\\(\\). However, because this will slow down your code and lock the user session until the response is received, it is generally recommended that you use getXML\\(\\) with a callback function.\n\n**Note:** Do not use AJAXEvaluateSynchronously.\n\nThe getXMLWait\\(\\) method is not available in scoped applications.\n\nThis code results in a client-side alert that displays `The Server Says Hello Bob!`.\n\nThe client code.\n\n```\nvar ga = new GlideAjax('HelloWorld') ;\nga.addParam('sysparm_name','helloWorld');\nga.addParam('sysparm_user_name',\"Bob\");\nga.getXMLWait(); \nalert(ga.getAnswer());\n```\n\nThe server-side script include code.\n\n```\nvar HelloWorld = Class.create();\nHelloWorld.prototype = Object.extendsObject(AbstractAjaxProcessor, {\nhelloWorld: function() \n { return \"The Server Says Hello \" + this.getParameter('sysparm_user_name') + \"!\"; } } );\n```\n\n## AJAXClientHelper\n\nProvides helper functions for Ajax clients to retrieve a value from an Ajax client.\n\n### Where to use\n\nUse this script include wherever your need to retrieve a value from an Ajax client.\n\n### Method summary\n\n|Method summary|Description|\n|--------------|-----------|\n|getDisplay\\(\\)|Gets the display value from the choice list.|\n\n### Method detail\n\n|API method|Description|Input parameters|Output returns|\n|----------|-----------|----------------|--------------|\n|getDisplay\\(\\)|Gets the display value from the choice list.|none|The display value.|\n\n### Curl\n\n```\n// getDisplay\n(function(table, sysId) {\n    var ga = new GlideAjax('AjaxClientHelper');\n    ga.addParam(\"sysparm_name\", \"getDisplay\");\n    ga.addParam('sysparm_table', 'incident');\n    ga.addParam('sysparm_value', \"0598b22b877313003c1c8467a7cb0b71\");\n    ga.getXMLWait();\n    return ga.getAnswer();\n}(\"incident\", \"0598b22b877313003c1c8467a7cb0b71\")); // Returns 'INC0010001'\n```\n\n"}}