Alex Roytman: Welcome back! Today, I have with me Eric Cassidy, a developer here at Profound Logic.
Eric Cassidy: Hello!
Alex Roytman: As you’re starting to work with AJAX, you’re maybe curious to find out how to respond with multiple values from AJAX request. In our previous example, we passed a product I'd and we responded with the product description. But what if you also wanted to send along the price and want it to be on hand.
Eric Cassidy: Your entire record or set of records?
Alex Roytman: Exactly. I attended an AJAX session—and in the session, they instructed and did provide an example of how to respond with multiple values but it really used a separated character and then Java script was used to parse the string out and put the multiple values into an array and it really seem like there’s got to be a better way than to do something like this.
Eric Cassidy: Well, there is a better way. And that’s Java Script Object Notation or JSON.
Alex Roytman: And JSON is just such a good way to handle something like this. Alright, Eric would you tell us some more about JSON and show us some examples.
Eric Cassidy: Sure. To begin, let’s review how an AJAX request responds with one value only. It’s very simple. We use RPG to retrieve a record and then we output the appropriate field value directly to standard out. Now, let’s say we wanted to pass an array of values, using JSON or Java Script Object Notation, here is what the syntax looks like. We use brackets to signify an array and we separate the values. When done this way, no special parsing has to be done on the front end when the response is received. This is because we use Java Script’s native syntax to represent an array. But JSON responds as automatically evaluated and we can start utilizing the array in our AJAX handler function.
In addition to arrays, JSON can also be utilized to send objects with properties. Typically, this is what we use to represent records of information. Let’s modify our example to send a product’s description, price and quantity as an object. Instead of brackets, we use curly braces. And each value is prefaced by a property name and a colon. Just like with arrays, objects can be utilized directly in Java Script without any special parsing. We use the dot syntax to retrieve each property. And finally, it’s possible to embed objects within arrays or arrays with an object or any combination there. This allows us to send multiple records. The example on your screen sends all records within our products file as a response to an AJAX request. The brackets at the top and bottom indicate that an array of records is coming. We read through all the records using a Do-While Loop. And each record is then represented as an object with name properties enclosed in curly braces.
As in my previous examples, this response will be consumed directly by Java Script. It will automatically become an array of objects without requiring you to parse the output manually. Back to you Alex.
Alex Roytman: Thank you Eric. For more information on this and other topics relating to IBM i modernization and web development, please visit www.profoundlogic.tv.
Transcription by:
Scribe4you Transcription Services