x6.json
The Javascript object x6.json is used to send any request
to the web server, either for a new complete page or for
data and HTML fragments.
Examples include requesting a single row from a table,
requesting search results, fetching HTML fragments, or
popping up a new window.
Andromeda uses the term JSON instead of AJAX because
the term AJAX does not describe how Andromeda works.
Specifically, Andromeda PHP pages return JSON data
instead of XML (hence no "X" in AJA"X"), and many calls
are actually synchronous (hence no "A" in "A"JAX).
The x6.json object is always present on all pages, and
you can use it in Javascript code on any custom page.
Andromeda handles all of the values returned in the
request automatically. On custom pages you do not have
to code a response handler because Andromeda handles
the response for you.
The PHP code that handles a JSON request sends data back
by using the routines x4Debug, x4Data, x4HTML, x4Script,
x4Error. While the complete PHP-API documentation on
those functions will give you most of what you need, we
must note here how the returned data is handled:
The basic usage of x6.json is to initialize a call
with x6.json.init, and then to add parameters with
x6.json.addParm, and finally to execute and process the
call with x6.json.execute and x6.json.process.
There are also special-purpose methods like x6.json.inputs
that will take all of the inputs inside of an object and
add them to the request.
You can also use the function x6.json.windowLocation to
execute the call as a new page request, and x6.json.newWindow
to execute the call as a new page request in a tab.
<script>
// Initialize the call
x6.json.init('x4Page','myCustomPage');
// Name the server-side PHP method to call
x6.json.addParm('x4Action','getSomething');
// Add some parms
x6.json.addParm('parm1','value');
x6.json.addParm('parm2','value');
// Execute and process in one step. Note that this
// is synchronous, there is no need for a callback
// function.
x6.json.execute(true);
for(var x in x6.data.returnedStuff) {
....
}
</script>
This call requires an Extended-Desktop page to be defined
in PHP that will service the request. A super-simple example
is here, more information is provided in the
Extended-Desktop documentation.
<?php
# This is file application/x4MyCustomPage.php
class x4MyCustomPage extends androX4 {
# this function handles the call given above
function getSomething() {
$parm1 = gp('parm1');
$parm2 = gp('parm2');
$sql = "Select blah blah blah";
$rows = SQL_AllRows($sql);
x4Data('returnedStuff',$rows
}
}
?>
Sometimes you make a call that returns replacement HTML
for a single object. In this case your PHP code supplies
the HTML by calling x4HTML with the value of '*MAIN*' for
the first parameter, as in x4HTML('*MAIN*',$html);
Such a call is handled this way in script:
<script>
x6.json.init('x4Page','myCustomPage');
// We need the conditional in case the server returns
// an error and we should not replace the html
if(x6.json.execute()) {
x6.json.process('nameofItemToReplace');
}
</script>
Child Topics