SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Build Your Own Ajax Web Applications help

    Alright, I'm finally taking a stab at Ajax and I have an annoying problem:

    Here is the code for the ajax object:
    Code:
    function Ajax()
    {
      this.req            = null;
      this.url            = null;
      this.method         = 'GET';
      this.async          = true;
      this.status         = null;
      this.statusText     = '';
      this.postData       = null;
      this.readyState     = null;
      this.responseText   = null;
      this.responseXML    = null;
      this.handleResp     = null;
      this.responseFormat = 'text'; // 'text', 'xml', or 'object'
      this.mimeType       = null;
      
      // Methods
      //  Build the request object
      this.init = function()
      {
        if(!this.req)
        {
          try
          {
            //  Try to create an object for Firefox, Safari, IE7, etc.
            this.req = new XMLHttpRequest();
          }
          catch(e)
          {
            try
            {
              //  Try to create an object for later versions of IE.
              this.req = new ActiveXObject('MSXML2.XMLHTTP');
            }
            catch(e)
            {
              try
              {
                //  Try to create an object for earlier versions of IE.
                this.req = new ActiveXObject('Microsoft.XMLHTTP');
              }
              catch(e)
              {
                //  Could not create the XHLHttpRequest object
                return false;
              }
            }
          }
        }
        return this.req;
      };
      
      //  Make the request
      this.doReq = function()
      {
        if(!this.init())
        {
          alert('Could not create an XMLHttpRequest object');
          return;
        }
    
        this.req.open(this.method, this.url, this.async);
        if(this.mimeType)
        {
          try
          {
            this.req.overrideMimeType(this.mimeType);
          }
          catch(e)
          {
            // Could not override MIME type --- IE6 or Opera?
          }
        }
        var self = this; // Fix the loss of scope in the inner function
        
        //  Check the state change
        this.req.onreadystatechange = function()
        {
          var resp = null;
          if(self.req.readyState == 4)
          {
            switch(self.responseformat)
            {
              case 'text':
                resp = self.req.responseText;
                break;
              case 'xml':
                resp = self.req.responseXML;
                break;
              case 'object':
                resp = req;
                break;        
            }
            if(self.req.status >= 200 && self.req.status <= 299)
            {
              self.handleResp(resp);
            }
            else
            {
              self.handleErr(resp);
            }
          }
        };
        this.req.send(this.postData);
      };
      
      //  Set the mime type
      this.setMimeType = function(mimeType)
      {
        this.mimeType = mimeType;
      };
      
      //  Error handling
      this.handleErr = function()
      {
        var errorWin;
        try
        {
          errorWin = window.open('', 'errorWin');
          errorWin.document.body.innerHTML = this.responseText;
        }
        catch(e)
        {
          alert('An error has occurred, but the error message cannot be '
          + 'displayed.  this is probably because of your browser\'s '
          + 'pop-up blocker. \n'
          + 'Please allow pop-ups from this web site if you want to '
          + 'see the full error messages.\n'
          + '\n'
          + 'Status Code: ' + this.req.status + '\n'
          + 'Status Desription: ' + this.req.statusText);
        }
      };
      
      //  Set error handler?
      this.setHandleErr = function(funcRef)
      {
        this.handleErr = funcRef;
      }
      
      //  Set the handler for errors and responses
      this.setHandlerBoth = function(funcRef)
      {
        this.handleResp = funcRef;
        this.handleErr = funcRef;
      };
      
      //  Abort the request
      this.abort = function()
      {
        if(this.req)
        {
          this.req.onreadystatechange = function() {};
          this.req.abort();
          this.req = null;
        }
      };
      
      //  This is what we do with the response
      this.doGet = function(url, hand, format)
      {
        this.url = url;
        this.handleResp = hand;
        this.responseFormat = format || 'text';
        this.doReq();
      };
    }
    Here is the html page:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//ENG" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>A Simple AJAX Test</title>
        <script type="text/javascript" src="ajaxObject.js"></script>
        <script type="text/javascript">
          var hand = function(str)
          {
            alert(str);
          }
          
          var ajax = new Ajax();
          ajax.doGet('fakeserver.php', hand);
        </script>
      </head>
      <body>
      </body>
    </html>
    And the fakeserver.php file:

    Code:
    <?php
    
      header('Content-Type: text/plain');
      sleep(rand(3, 12));
      print 'Ok';
    
    ?>
    The alert I get back is "null", not "Ok", and I'm not seeing the problem. any help would be greatly appreciated!
    John

  2. #2
    Guru in training bronze trophy SoulScratch's Avatar
    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    1,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
            switch(self.responseformat)
    This should be in uppercase. Remember, camelCase.

  3. #3
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    LOL!!! Figures.

    Thanks so much!
    John


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •