Join this online group to communicate across IBM product users and experts by sharing advice and best practices with peers and staying up to date regarding product enhancements.
This example shows AJAX code in a JSP page that is used to get the login status.
This simple example displays a number of interesting characteristics, called out in the code that follows:
<html> // #1 <head> <script type="text/javascript"> function getInfo() { // Create a communications object. var xmlhttp; if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); } // Configure the comms object with the function that runs when a response is received. xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { // Success. Insert returned data into the page. text = "<pre>" + xmlhttp.responseText + "</pre>"; var reply = eval('(' + xmlhttp.responseText + ')'); // #2 result = reply.platform.user.is_session_valid; text += "Result: " + result; document.getElementById("myDiv").innerHTML=text; } } // Set up the request and send it to the server resource = "/networking/rest/user/isSessionValid?alt=json"; // #3 async = true; xmlhttp.open("GET", resource, async); xmlhttp.send(); } </script> </head> <body> <div id="myDiv"><h2>Click the button to check status.</h2></div> <button type="button" onclick="getInfo()">Ok</button> </body> </html>
Visiting the page and clicking the button echoes the response returned by server:
{"platform": { // #2 "message": { "code": "0", "description": "Success" }, "user": {"is_session_valid": "true"} // #4 }} Result: true