The response to an AJAX request can be anything at all it to email text even an image.
It's up to the javascript code that handles the request to deal with whatever is sent back.
With the example we just saw we returned a simple string of text which we inserted directly into the
h.t. AML when it was returned to the Ajax request.
But what if we want to return something more complex than a single value like this.
Let's look at another example in this script.
We've got an associative array of values that might represent a user with elements like an I.D. name
email address and so on.
While we're doing here at the moment is using the VAR dump to print out the entire array.
If we view this in the browser we see the contents of the array.
We want to display this data on this page where will use a d l element to display it.
The d l r description list element is commonly used to display a list of key value pairs.
Just like the user data we've got
as in the previous example we've got a button aliment and we using J query to handle the button click
when the button is correct.
The script sends an AJAX request to the get data page.
And for now we'll just display this data in an alert dialogue.
If I click the button we see the data coming back from the script.
We can't really do anything with the data like this though.
As javascript doesn't understand the output from the P HP var dump function all it sees is a string.
The solution is to use a standard format for into changing data when using Ajax.
The format to use is Jason.
Are javascript object notation.
Jason is basically a way to format data fast sending from one place to another.
For example from a web server to a web browser.
Although it's based on Javascript it is language independent so we can use it in different languages
easily in P H.
P we have functions for converting data into the adjacent format and converting it back again and in
javascript.
We have similar functions.
So instead of using var dump to print this data out will echo out the data encoded as Jason instead.
In the browser the Jason data looks like this.
Basically just a simple array of key value pairs.
In the Ajax request when we get this data which is now Jason first we need to use the Jason up pass
function to decode the Jason data.
Then let's show this data.
In the alert box for now.
When we make the Ajax request what's shown in the dialogue is an object.
When the Jason is decoded an object is created.
And in this case it'll be a simple object with properties for each of the original array elements.
So for example let's display the name property.
And now when I click the button we get the name attribute from the original data.
So to display these attributes in the astm l..
First let's add I.D. to the description definition elements inside the description list.
Then instead of the alert we can get the name element and use the astm malfunction to insert the name
value directly into the astm l..
We can repeat this from the email and date of birth values.
So now when I click the button the attributes are inserted into each d d element.
So whenever you're using AJAX to get some data from the server if that data is anything other than a
simple value the best way to send that data is encoding it as Jason and then decoding that Jason.
When the data are received.