Sidebar

AJAX Isn't Just for Cleaning Dishes Anymore

Web Languages
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

Until recently, moving to browser-based RPG IV applications has been limited because Web pages have to be reloaded or new pages need to be displayed in order to give the end-user the appropriate feedback. Users never really had the same quality experience as they had with a 5250 green-screen applications.

For example, if you provide users with a file maintenance application in 5250, they may get a prompt that says "Type the Customer Number, then press Enter." When they type in an invalid customer number, a message such as "Customer 5678 not found" is displayed, and the cursor is positioned in the customer number input field. Normally, the field would also be set to reverse-image or highlighted in some way. When users type in a valid customer number, the data is displayed and can be updated as necessary. When the users finish making the changes, they press Enter and get immediate feedback. For example: "Address line 1 cannot be blank" or "Customer changes successful."

With a typical Web- or browser-based application, the user types in a customer number on a Web page, which calls an RPG IV program to check if that customer exists. If the customer is not found, another Web page is created with the message "Customer 5689 not found." It is displayed in that same browser window along with the infamous "Press the Back button in your browser to fix this error" message. Tragic.

If the user types in a good customer number, a new Web page is displayed showing the customer's data. The user makes any changes and then presses the Submit button. Some JavaScript may be used on that page to edit-check the input fields before passing the data along to another RPGIV program that performs the final validation and records the changes. If there is another error, the user once again sees "Press the Back button in your browser to fix this error."

While this does provide the end-users with a conventional browser interface, it is not a fun experience.

I believe this common type of experience is one of the reasons a lot of people are staying away from the browser as a user interface. They see it as being better-looking but not a better end-user experience. This is not good for RPG IV programmers because, unless RPG IV programmers start moving nearly all apps to some form of GUI, there will be little future for the iSeries box and thus few employment opportunities for RPG programmers. See my related article on this topic in this week's issue.

Asynchronous JavaScript and XML

An emerging technology that is becoming the "next big thing" in application development isn't a new product or a new language (thank goodness!). Instead, it is a collection and organization of a couple of existing technologies that you already have. (Read: You don't have to buy anything new to make them work.) That technology is known as Asynchronous JavaScript and XML, or simply AJAX.

Two important facts about AJAX are 1) you don't have to buy anything to make it work, and 2) it allows you to create browser-based applications that behave very similarly to 5250-based applications, but with a graphical user experience. That is, you create applications that use one page rather than several different pages to give your end-users a much more refined experience.

A side effect is that you avoid all those page refreshes and pop-up windows. And you don't need to have multiple browser windows open for an application. Everything is done in one page, or two or three, similar to 5250 display file-based applications.

Let's break down AJAX and look at what it is.

AJAX uses asynchronous JavaScript to communicate with your server without requiring the current page to send data to a server via a Submit button. The JavaScript in the existing page posts a request to your RPG IV program, and it waits for a response in the background while it releases the browser back to the user. The user never knows what's going on in the background (unless you want him to).

In other words, it effectively uses the idle time in the browser to send the customer number (from our previous example) up to the server and get feedback as to whether or not it was a good customer number. When the JavaScript receives the response, it can do whatever it wants, including writing HTML or text to the existing page. That way, the server doesn't need to respond with a new Web page or a CGI request that outputs an entirely new page; instead, it returns information to the current browser page. That data is used to update the current page.

For example, a message that says "Customer not found. Try again" could be displayed, and the cursor could be automatically positioned in the customer number input field. I also like to change the background color of the input field when it is causing an error.

The key to all of this is through the use of the JavaScript XMLHttpRequest object. To use this object, you simply declare an instance of it. It does depend on which browser you're using. There are several methods to create this object correctly; the one I use most is shown below. However, any method you prefer will probably be OK.

The following illustrates the GoAjax() function I wrote to generically evoke an AJAX request in an HTML page.



  var req;

function GoAjax(myUrl) {

   if (window.XMLHttpRequest) {  // FireFox, Safari,...
        req = new XMLHttpRequest();
        if (req.overrideMimeType) {
            req.overrideMimeType('text/xml');
        }
    } else if (window.ActiveXObject) { // Microsoft IE
        try {
              req = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
          try {
             req = new ActiveXObject("Microsoft.XMLHTTP");
          } catch (e) {}
        }
    }

    if (!req) {
        alert('Darn it! No XMLHTTP request.');
        return false;
    }
                
    req.onreadystatechange = myCallback;
    req.open('GET', myUrl, true);
    req.send(null);
}

This JavaScript code creates an HTTP Request object and then uses the GET method to open a dialog with the Web server at the designated URL. The HTTP Request is first attempted the Mozilla way (NetScape/FireFox/Safari) using the XMLHttpRequest object. If that fails, the code attempts to create the object using Microsoft's ActiveXObject. Of course, it wouldn't be Microsoft if there weren't two versions of its stuff in production. The good news is that the resulting object is the same, regardless of Mozilla or Microsoft methods. Once it is created, you can use it the same way throughout the rest of the process.

In order to allow the end-user to continue to use the browser while the request is sent off to the iSeries, we need to do two simple things: 1) We need to tell the created object the name of a function to call when the request receives a reply from the server, and 2) we need to send the request with the async attribute set on.

To indicate the name of the function to call when the server responds, the onreadystatechange property of the XMLHttpRequest object is used. This field holds the so-called "callback procedure." It gets its name from the fact that you're telling the process to "call back" when it is finished. What gets called back is the function whose name you provide.

This is performed in a three-step process. The following JavaScript code in our GoAjax() function accomplishes these three steps:

    req.onreadystatechange = myCallback;
    req.open('GET', myUrl, true);
    req.send(null);

Step 1: Assigning a Callback Function

In JavaScript, you assign a callback by assigning the name to the onreadystatechange property of the XMLHttpRequest object, as follows:

    req.onreadystatechange = myCallback;

In this example, myCallback is the name of the callback function. This function is called when the server responds to the AJAX request.

A typical callback routine will check the state of the response and react accordingly. There are five possible states (known as the "ready states" ) for a response:

  • 0 = uninitialized
  • 1 = loading
  • 2 = loaded
  • 3 = interactive
  • 4 = complete

Effectively, the only ready state you care about is 4. This tells you that the request has completed and you may begin to extract the data sent to you by the server. Here's a simplified JavaScript callback function that tests the ready state of the request:

 function  myCallback() {
    if (req.readyState == 4) {
       if (req.status == 200) {
          myResponseFunc(req.responseXML);
        }
    }
 }

In this example, the ready state is checked. If it is equal to 4, then it checks the status of the response. If status 200 comes back, the requested URL passed to the server on the req.open() function was successful. At this point, we know everything should be OK, and we can call a user-written function to handle the data that the server sends to the browser.

Step 2: Open a Connection to the Server

To cause the asynchronous request to be issued, you have to pass the CGI "method" the URL-encoded string and indicate that you want an asynchronous request. The following line of JavaScript does this for us.

    req.open('GET', myUrl, true);

The first parameter of the req.open function is 'GET', which indicates that we want to use the CGI GET method to send the request to the server. This is the same GET method as the GET method used on HTML forms. The other options are POST and HEAD, which also send the requests to the server but cause different results. The HEAD method simply requests that only the HTTP headers be returned. Typically, you don't care about the HEAD method. The POST method causes the data to be sent through a channel that is hidden from snoopers; it is not stored in the browser's history file.

The second parameter of the req.open function is the URL you want to send to the server. This URL should be encoded/escaped before it is sent. Normally, you can encode URLs using the escape() function, but newer versions of JavaScript support encodeURL() and encodeURLComponent(). I use the following JavaScript function to check for which version (escape or encodeURIComponent) is installed and call it accordingly.

function encodeMe(value)
{
   if (encodeURIComponent) {
     return encodeURIComponent(value);
   }
   if (escape) {
       return escape(value);
   }
   return value;
}

In my JavaScript, I would normally do something like this to encode a value being added to a URL string:

   var theText  = document.CUSTMAIN.CSTNAME.value;
   var url = "/cgi-bin/myCGIPGM?CSTNAME=" + encodeMe(theText);

The third parameter of the open function indicates whether you want synchronous or asynchronous processing. If it's unspecified or if "false" is specified, synchronous processing is performed. If "true" is specified, asynchronous processing is performed.

Synchronous requests can be useful, but not for what we're trying to accomplish, so make sure you specify "true" for this parameter; otherwise, you'll end up with unhappy users waiting while the server is working—not a nice thing to do to someone. Besides, it's called AJAX, not SJAX.

Step 3: Send the Asynchronous Request

If you need to send more information to the server's CGI program, you may do so using the send() function of the XMLHttpRequest object. Otherwise, you need to indicate that nothing else is being sent and that it's OK to start performing your request. So you need to send nothing. Do this by passing "null" as the parameter for the send() function, as follows:

    req.send(null);

Now the request is sent, and if everything was done correctly, your RPG IV CGI program has been called and can start building the response.

Your RPG IV program responds either by sending nicely formatted XML data or by sending plain text. Either way, the JavaScript in your HTML page will need to extract or parse this data and then update the Web page.

Parsing the response and updating the Web page are topics for the next article on this important topic.

Now a Little Bad News

Unfortunately, some users are blocking scripts in their browsers. We IT staffers often use this as a reason to avoid JavaScript or VBScript in our HTML pages. I blame this on associative post-dramatic stress syndrome (not sure if that's a real term or one that I just made up). But it seems to me that if we have a problem on a PC due to some random security hole potentially caused by ActiveX controls or VBScripting in Internet Explorer, we fight it by blocking all scripting in all languages in everyone's browser. Sort of like killing all the chickens in Arkansas because a few chickens in Asia had the flu. In other words, it's overkill.

The good news is the current release of some of the browsers and the next release of many others offer selective Script blocking. This allows you to block Web sites from running scripts just like you now block email domains with your junk email filters. So you can allow scripts to run on the good Web sites, like mcpressonline.com or rpgiv.com, and block them on the not-so-good sites. This means that scripting, in particular JavaScript, can be supported and relied on in your day-to-day browser-based applications.

Google, arguably the biggest single advocate of JavaScript and AJAX use in the world today, uses JavaScript everywhere. In fact, I've already done consulting/contracting with two customers who wanted to add a "Google Suggest" style of interface to a browser-based application. I must admit, once we got the HTML looking the way they wanted it, it produced some very impressive results, especially considering it was iSeries and RPG IV behind it all.

Now that we have a GUI alternative to DDS that everyone already owns, RPG programming is starting to be cool again! Get on board and have fun!

This is the first in a series of articles on AJAX programming with iSeries, so stayed tuned. And watch for my new book about AJAX with iSeries, coming soon from MC Press.

Bob Cozzi is a programmer/consultant, writer/author, and software developer of the RPG xTools, a popular add-on subprocedure library for RPG IV. His book The Modern RPG Language has been the most widely used RPG programming book for nearly two decades. He, along with others, speaks at and runs the highly-popular RPG World conference for RPG programmers.

Robert Cozzi

Bob Cozzi is a programmer/consultant, writer/author, and software developer. His popular RPG xTools add-on subprocedure library for RPG IV is fast becoming a standard with RPG developers. His book The Modern RPG Language has been the most widely used RPG programming book for more than a decade. He, along with others, speaks at and produces the highly popular RPG World conference for RPG programmers.


MC Press books written by Robert Cozzi available now on the MC Press Bookstore.

RPG TnT RPG TnT
Get this jam-packed resource of quick, easy-to-implement RPG tips!
List Price $65.00

Now On Sale

The Modern RPG IV Language The Modern RPG IV Language
Cozzi on everything RPG! What more could you want?
List Price $99.95

Now On Sale

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

RESOURCE CENTER

  • WHITE PAPERS

  • WEBCAST

  • TRIAL SOFTWARE

  • White Paper: Node.js for Enterprise IBM i Modernization

    SB Profound WP 5539

    If your business is thinking about modernizing your legacy IBM i (also known as AS/400 or iSeries) applications, you will want to read this white paper first!

    Download this paper and learn how Node.js can ensure that you:
    - Modernize on-time and budget - no more lengthy, costly, disruptive app rewrites!
    - Retain your IBM i systems of record
    - Find and hire new development talent
    - Integrate new Node.js applications with your existing RPG, Java, .Net, and PHP apps
    - Extend your IBM i capabilties to include Watson API, Cloud, and Internet of Things


    Read Node.js for Enterprise IBM i Modernization Now!

     

  • Profound Logic Solution Guide

    SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation.
    Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects.
    The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the companyare not aligned with the current IT environment.

    Get your copy of this important guide today!

     

  • 2022 IBM i Marketplace Survey Results

    Fortra2022 marks the eighth edition of the IBM i Marketplace Survey Results. Each year, Fortra captures data on how businesses use the IBM i platform and the IT and cybersecurity initiatives it supports.

    Over the years, this survey has become a true industry benchmark, revealing to readers the trends that are shaping and driving the market and providing insight into what the future may bring for this technology.

  • Brunswick bowls a perfect 300 with LANSA!

    FortraBrunswick is the leader in bowling products, services, and industry expertise for the development and renovation of new and existing bowling centers and mixed-use recreation facilities across the entertainment industry. However, the lifeblood of Brunswick’s capital equipment business was running on a 15-year-old software application written in Visual Basic 6 (VB6) with a SQL Server back-end. The application was at the end of its life and needed to be replaced.
    With the help of Visual LANSA, they found an easy-to-use, long-term platform that enabled their team to collaborate, innovate, and integrate with existing systems and databases within a single platform.
    Read the case study to learn how they achieved success and increased the speed of development by 30% with Visual LANSA.

     

  • Progressive Web Apps: Create a Universal Experience Across All Devices

    LANSAProgressive Web Apps allow you to reach anyone, anywhere, and on any device with a single unified codebase. This means that your applications—regardless of browser, device, or platform—instantly become more reliable and consistent. They are the present and future of application development, and more and more businesses are catching on.
    Download this whitepaper and learn:

    • How PWAs support fast application development and streamline DevOps
    • How to give your business a competitive edge using PWAs
    • What makes progressive web apps so versatile, both online and offline

     

     

  • The Power of Coding in a Low-Code Solution

    LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed.
    Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

    • Discover the benefits of Low-code's quick application creation
    • Understand the differences in model-based and language-based Low-Code platforms
    • Explore the strengths of LANSA's Low-Code Solution to Low-Code’s biggest drawbacks

     

     

  • Why Migrate When You Can Modernize?

    LANSABusiness users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.
    In this white paper, you’ll learn how to think of these issues as opportunities rather than problems. We’ll explore motivations to migrate or modernize, their risks and considerations you should be aware of before embarking on a (migration or modernization) project.
    Lastly, we’ll discuss how modernizing IBM i applications with optimized business workflows, integration with other technologies and new mobile and web user interfaces will enable IT – and the business – to experience time-added value and much more.

     

  • UPDATED: Developer Kit: Making a Business Case for Modernization and Beyond

    Profound Logic Software, Inc.Having trouble getting management approval for modernization projects? The problem may be you're not speaking enough "business" to them.

    This Developer Kit provides you study-backed data and a ready-to-use business case template to help get your very next development project approved!

  • What to Do When Your AS/400 Talent Retires

    FortraIT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators is small.

    This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn:

    • Why IBM i skills depletion is a top concern
    • How leading organizations are coping
    • Where automation will make the biggest impact

     

  • Node.js on IBM i Webinar Series Pt. 2: Setting Up Your Development Tools

    Profound Logic Software, Inc.Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. In Part 2, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Attend this webinar to learn:

    • Different tools to develop Node.js applications on IBM i
    • Debugging Node.js
    • The basics of Git and tools to help those new to it
    • Using NodeRun.com as a pre-built development environment

     

     

  • Expert Tips for IBM i Security: Beyond the Basics

    SB PowerTech WC GenericIn this session, IBM i security expert Robin Tatam provides a quick recap of IBM i security basics and guides you through some advanced cybersecurity techniques that can help you take data protection to the next level. Robin will cover:

    • Reducing the risk posed by special authorities
    • Establishing object-level security
    • Overseeing user actions and data access

    Don't miss this chance to take your knowledge of IBM i security beyond the basics.

     

     

  • 5 IBM i Security Quick Wins

    SB PowerTech WC GenericIn today’s threat landscape, upper management is laser-focused on cybersecurity. You need to make progress in securing your systems—and make it fast.
    There’s no shortage of actions you could take, but what tactics will actually deliver the results you need? And how can you find a security strategy that fits your budget and time constraints?
    Join top IBM i security expert Robin Tatam as he outlines the five fastest and most impactful changes you can make to strengthen IBM i security this year.
    Your system didn’t become unsecure overnight and you won’t be able to turn it around overnight either. But quick wins are possible with IBM i security, and Robin Tatam will show you how to achieve them.

  • Security Bulletin: Malware Infection Discovered on IBM i Server!

    SB PowerTech WC GenericMalicious programs can bring entire businesses to their knees—and IBM i shops are not immune. It’s critical to grasp the true impact malware can have on IBM i and the network that connects to it. Attend this webinar to gain a thorough understanding of the relationships between:

    • Viruses, native objects, and the integrated file system (IFS)
    • Power Systems and Windows-based viruses and malware
    • PC-based anti-virus scanning versus native IBM i scanning

    There are a number of ways you can minimize your exposure to viruses. IBM i security expert Sandi Moore explains the facts, including how to ensure you're fully protected and compliant with regulations such as PCI.

     

     

  • Encryption on IBM i Simplified

    SB PowerTech WC GenericDB2 Field Procedures (FieldProcs) were introduced in IBM i 7.1 and have greatly simplified encryption, often without requiring any application changes. Now you can quickly encrypt sensitive data on the IBM i including PII, PCI, PHI data in your physical files and tables.
    Watch this webinar to learn how you can quickly implement encryption on the IBM i. During the webinar, security expert Robin Tatam will show you how to:

    • Use Field Procedures to automate encryption and decryption
    • Restrict and mask field level access by user or group
    • Meet compliance requirements with effective key management and audit trails

     

  • Lessons Learned from IBM i Cyber Attacks

    SB PowerTech WC GenericDespite the many options IBM has provided to protect your systems and data, many organizations still struggle to apply appropriate security controls.
    In this webinar, you'll get insight into how the criminals accessed these systems, the fallout from these attacks, and how the incidents could have been avoided by following security best practices.

    • Learn which security gaps cyber criminals love most
    • Find out how other IBM i organizations have fallen victim
    • Get the details on policies and processes you can implement to protect your organization, even when staff works from home

    You will learn the steps you can take to avoid the mistakes made in these examples, as well as other inadequate and misconfigured settings that put businesses at risk.

     

     

  • The Power of Coding in a Low-Code Solution

    SB PowerTech WC GenericWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed.
    Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

    • Discover the benefits of Low-code's quick application creation
    • Understand the differences in model-based and language-based Low-Code platforms
    • Explore the strengths of LANSA's Low-Code Solution to Low-Code’s biggest drawbacks

     

     

  • Node Webinar Series Pt. 1: The World of Node.js on IBM i

    SB Profound WC GenericHave you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.
    Part 1 will teach you what Node.js is, why it's a great option for IBM i shops, and how to take advantage of the ecosystem surrounding Node.
    In addition to background information, our Director of Product Development Scott Klement will demonstrate applications that take advantage of the Node Package Manager (npm).
    Watch Now.

  • The Biggest Mistakes in IBM i Security

    SB Profound WC Generic The Biggest Mistakes in IBM i Security
    Here’s the harsh reality: cybersecurity pros have to get their jobs right every single day, while an attacker only has to succeed once to do incredible damage.
    Whether that’s thousands of exposed records, millions of dollars in fines and legal fees, or diminished share value, it’s easy to judge organizations that fall victim. IBM i enjoys an enviable reputation for security, but no system is impervious to mistakes.
    Join this webinar to learn about the biggest errors made when securing a Power Systems server.
    This knowledge is critical for ensuring integrity of your application data and preventing you from becoming the next Equifax. It’s also essential for complying with all formal regulations, including SOX, PCI, GDPR, and HIPAA
    Watch Now.

  • Comply in 5! Well, actually UNDER 5 minutes!!

    SB CYBRA PPL 5382

    TRY the one package that solves all your document design and printing challenges on all your platforms.

    Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product.

    Request your trial now!

  • Backup and Recovery on IBM i: Your Strategy for the Unexpected

    FortraRobot automates the routine tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:
    - Simplified backup procedures
    - Easy data encryption
    - Save media management
    - Guided restoration
    - Seamless product integration
    Make sure your data survives when catastrophe hits. Try the Robot Backup and Recovery Solution FREE for 30 days.

  • Manage IBM i Messages by Exception with Robot

    SB HelpSystems SC 5413Managing messages on your IBM i can be more than a full-time job if you have to do it manually. How can you be sure you won’t miss important system events?
    Automate your message center with the Robot Message Management Solution. Key features include:
    - Automated message management
    - Tailored notifications and automatic escalation
    - System-wide control of your IBM i partitions
    - Two-way system notifications from your mobile device
    - Seamless product integration
    Try the Robot Message Management Solution FREE for 30 days.

  • Easiest Way to Save Money? Stop Printing IBM i Reports

    FortraRobot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing.
    Manage your reports with the Robot Report Management Solution. Key features include:

    - Automated report distribution
    - View online without delay
    - Browser interface to make notes
    - Custom retention capabilities
    - Seamless product integration
    Rerun another report? Never again. Try the Robot Report Management Solution FREE for 30 days.

  • Hassle-Free IBM i Operations around the Clock

    SB HelpSystems SC 5413For over 30 years, Robot has been a leader in systems management for IBM i.
    Manage your job schedule with the Robot Job Scheduling Solution. Key features include:
    - Automated batch, interactive, and cross-platform scheduling
    - Event-driven dependency processing
    - Centralized monitoring and reporting
    - Audit log and ready-to-use reports
    - Seamless product integration
    Scale your software, not your staff. Try the Robot Job Scheduling Solution FREE for 30 days.