0% found this document useful (0 votes)
373 views

1.1 Section 07 - GlideAjax PDF

The document provides an overview of using GlideAjax in ServiceNow. GlideAjax enables making asynchronous client-side requests to server-side code in script includes. It uses the browser's XMLHttpRequest API to call script includes, with responses returned in XML by default. Script includes contain reusable server-side code that can be invoked from the client-side using GlideAjax or from other server-side locations. The document outlines the stages of a GlideAjax request and provides examples of client-side and server-side code.

Uploaded by

camis_vieira
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
373 views

1.1 Section 07 - GlideAjax PDF

The document provides an overview of using GlideAjax in ServiceNow. GlideAjax enables making asynchronous client-side requests to server-side code in script includes. It uses the browser's XMLHttpRequest API to call script includes, with responses returned in XML by default. Script includes contain reusable server-side code that can be invoked from the client-side using GlideAjax or from other server-side locations. The document outlines the stages of a GlideAjax request and provides examples of client-side and server-side code.

Uploaded by

camis_vieira
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Course Outline

1 Course Introduction 6 GlideForm & GlideUser

2 Development Overview 7 GlideAjax

3 Scripting Locations 8 Exploring Other APIs

4 GlideRecord 9 Becoming A Scripting Master

5 GlideSystem 10 Creating A Custom App


Section Outline
1 GlideAjax Introduction 7 Revisiting Script Includes

2 AJAX 8 The JavaScript Callback

3 Synchronous vs Asynchronous 9 JSON vs XML

4 GlideAjax Stages 10 GlideAjax API Overview & Methods

5 Example: GlideAjax Process 11 GlideAjax Demo

6 Show Me The Code! 12 Section Recap


GlideAjax Introduction




Client-side
Used to call server-side Script Includes
Similar to jQuery’s ajax method
“ The GlideAjax class
enables a client script to
call server-side code in a
script include.


ServiceNow Docs
AJAX

● Asynchronous JavaScript and XML


● Popularized by Google in Google Maps and Gmail
● Way to make client-side requests to server-side
without requiring a “page reload”
● Uses browser’s XMLHttpRequest API
Synchronous Versus Asynchronous
Synchronous Asynchronous
Client Server Client Server

Request 1 Request 1

Request 2
Response 1

Request 2 Response 1

Response 2 Response 2
3 Stages of GlideAjax
1 Client 3
Client-side code calls Client-side code
GlideAjax API, which processes the
makes a XMLHttpRequest response
to server

Server 2
Server-side code
processes the request
and returns a response
2 Scripting Locations To GlideAjax
1 Client-side code 2 Server-side code

Client Server

Client Script Script Include


UI Page
Example: GlideAjax Process

1 Client makes request for a page that contains a Client Script with GlideAjax

Task form
Update
geolocation on
task
Example: GlideAjax Process

2 Server sends client task form data along with Client Script

Task data w/ Client Script


Example: GlideAjax Process

3 After an onLoad event occurs, the client side script is executed which calls the GlideAjax API

Task onLoad
event executes
Client Script
Example: GlideAjax Process

4 GlideAjax accesses browser’s XMLHttpRequest API and generates a request

GlideAjax

Browser’s XMLHttpRequest API


Example: GlideAjax Process

5 Browser’s XMLHttpRequest API sends geolocation data back to ServiceNow in the background

Sends request
Example: GlideAjax Process
Request from client invokes Script Include, where request data is used to call specific methods
6 with arguments. Then data is packaged up in the form of a response.

Calls Script Include,


processes request
and then packages
up as a response
Example: GlideAjax Process

7 Browser receives response from server side

Sends response
Example: GlideAjax Process

8 Client Script callback processes returned data and updates location field on task

Processes response
and renders data
Show Me The Code!
● Update the short description field of an incident to Hello world! on-load

1 Client-side code 2 Server-side code


It All Starts With GlideAjax 1
1. Create a new GlideAjax object
2. Add name of Script Include method as sysparm_name parameter
3. Call getXML() method and pass the name of the callback as an argument

Note: Start all parameter names with sysparm_


Revisiting Script Includes 2

● Run on the server-side


● Contain reusable snippets of code, making them modular
● Only executed when invoked from another source
○ Client-side using GlideAjax
○ Server-side using the new JavaScript operator
● May extend other JavaScript classes
● When used for GlideAjax
○ Must have Client callable set to true
○ Extends the AbstractAjaxProcessor class
○ Typically queries a table and returns record(s) to client-side as JSON
○ Has access to any variable from client-side that starts with sysparm_
○ Uses sysparm_name to invoke method in Script Include
Extending AbstractAjaxProcessor 2

● AbstractAjaxProcessor is an out-of-the-box Script Include


● Provides helper methods
● Client callable checkbox automatically generates required JavaScript
The GlideAjax Script Include 2

1. Method passed in sysparm_name gets invoked


2. Server-side scripts are ran
3. return statement ends server-side script execution
4. Response is packaged up as XML and sent to client
Returned Payload 3

● XML is returned to the client-side from server-side (even if JSON)


● Use the following to retrieve answer
○ response.responseXML.documentElement.getAttribute(‘answer’)
The JavaScript Callback 3

● A callback is a function that is passed as an argument, which is


then executed at a later time
● Once the client receives the response from the server, the callback
is invoked

Dive Deeper: For more information on JavaScript callbacks, checkout this article
JSON Or XML?

● JavaScript Object Notation ● Extensible Markup Language


● Very easy & fast to parse ● Uses nodes & node attributes
● Uses collections of key/value pairs ● Can be a pain to parse
● Supports arrays

Dive Deeper: Checkout this YouTube video for a deeper understanding on XML vs JSON
XML Versus JSON
XML JSON
GlideAjax getXMLAnswer() Method

● Shortcut to
○ getXML()
○ response.responseXML.documentElement.getAttribute(‘answer’)
GlideAjax API Overview

GlideAjax

addParam() getXML()

getXMLAnswer()
GlideAjax Methods

● addParam() ● getXML()
● getXMLAnswer()
Where Can I Use This?

Client Side Server Side

GlideForm GlideRecord

GlideUser Client Scripts UI Actions GlideSystem Business Rules UI Actions

GlideAjax GlideDateTime

UI Scripts Service Portal Script Includes Scheduled Jobs

Service Portal Web Services Workflows


Section Recap

● Use GlideAjax when you need to access server-side


data while on the client-side
● GlideAjax is available to you within any client-side
scripting location
● Use Script Includes to store the server-side code you’d
like to run with GlideAjax
● By default, XML is returned by the GlideAjax API
● Use APIs to encode/decode JSON

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy