Home > Articles > Programming > Java

  • Print
  • + Share This
From the author of

Making a Password-Protected AJAX Request

With this new PasswordManager and its associated methods, we can easily add a unique password to our AJAX requests. In order to do this, we’ll create a PHP file that will be used to display on the client side. The first thing that we’ll do in this new PHP file is import the PasswordManager, get its instance, and create a unique password:

<?
    require_once("classes/PasswordManager.class.php");
    $pwManager = PasswordManager::getInstance();
    $pwManager->createPassword();
?>

Now that we have the password, let’s import the JavaScript objects that make up our AJAX engine:

<script type="text/javascript" src="javascript/model/Ajax.js"></script>
<script type="text/javascript" src="javascript/model/HTTP.js"></script>
<script type="text/javascript" src="javascript/model/AjaxUpdater.js"></script>
<script type="text/javascript">
    function sendProtectedRequest()
    {
       AjaxUpdater.Update(’GET’, ’xmlServiceConnector.php’, displayResponse);
    }

    function displayResponse()
    {
       if(Ajax.checkReadyState(’loading’) == "OK")
       {
           alert(Ajax.request.responseText);
       }
    }
</script>

With this code in place, we have all of the objects that we need in order to make the request, and two methods to send and receive data on the server side. The sendProtectedRequest method is using the AjaxUpdater object to make a request on a file called xmlServiceConnector, which we’ll use to verify the request by checking to see whether the cookie exists; then it passes the displayResponse method as a callback. The xmlServiceConnector file is an intermediate file that verifies the password before delegating the request to the appropriate PHP class, which then could make a database connection. We won’t focus on the database connection part in this article, but we’ll create the intermediate file to understand how the password is verified on the server side.

  • + Share This
  • 🔖 Save To Your Account