SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Apr 2006
    Location
    Gold Coast, Australia
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Script works but gets an error, any ideas?

    I'm using the Ajax class from the book "build your own Ajax web application".

    Code:
    function Ajax() {
      this.req = null;
      this.url = null;
      this.status = null;
      this.statusText = '';
      this.method = 'GET';
      this.async = true;
      this.dataPayload = null;
      this.readyState = null;
      this.responseText = null;
      this.responseXML = null;
      this.handleResp = null;
      this.responseFormat = 'text'; // 'text', 'xml', 'object'
      this.mimeType = null;
      this.headers = [];
    
    
      this.init = function() {
        var i = 0;
        var reqTry = [
          function() { return new XMLHttpRequest(); },
          function() { return new ActiveXObject('Msxml2.XMLHTTP') },
          function() { return new ActiveXObject('Microsoft.XMLHTTP' )} ];
    
        while (!this.req && (i < reqTry.length)) {
          try {
            this.req = reqTry[i++]();
          }
          catch(e) {}
        }
        return true;
      };
      this.doGet = function(url, hand, format) {
        this.url = url;
        this.setHandlerResp(hand);
        this.responseFormat = format || 'text';
        this.doReq();
      };
      this.doPost = function(url, dataPayload, hand, format) {
        this.url = url;
        this.dataPayload = dataPayload;
        this.setHandlerResp(hand);
        this.responseFormat = format || 'text';
        this.method = 'POST';
        this.doReq();
      };
      this.doReq = function() {
        var self = null;
        var req = null;
        var headArr = [];
    
        if (!this.init()) {
          alert('Could not create XMLHttpRequest object.');
          return;
        }
        req = this.req;
        req.open(this.method, this.url, this.async);
        if (this.method == "POST") {
          this.req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        }
        if (this.method == 'POST') {
          req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        }
        self = this;
        req.onreadystatechange = function() {
          var resp = null;
          self.readyState = req.readyState;
          if (req.readyState == 4) {
    
            self.status = req.status;
            self.statusText = req.statusText;
            self.responseText = req.responseText;
            self.responseXML = req.responseXML;
    
            switch(self.responseFormat) {
              case 'text':
                resp = self.responseText;
                break;
              case 'xml':
                resp = self.responseXML;
                break;
              case 'object':
                resp = req;
                break;
            }
    
            if (self.status > 199 && self.status < 300) {
              if (!self.handleResp) {
                alert('No response handler defined ' +
                  'for this XMLHttpRequest object.');
                return;
              } else {
               self.handleResp(resp);
              }
            }
    
            else {
              self.handleErr(resp);
            }
          }
        }
        req.send(this.dataPayload);
      };
      this.abort = function() {
        if (this.req) {
          this.req.onreadystatechange = function() { };
          this.req.abort();
          this.req = null;
        }
      };
      this.handleErr = function() {
        var errorWin;
        // Create new window and display error
        try {
          errorWin = window.open('', 'errorWin');
          errorWin.document.body.innerHTML = this.responseText;
        }
        // If pop-up gets blocked, inform user
        catch(e) {
          alert('An error occurred, but the error message cannot be' +
          ' displayed because of your browser\'s pop-up blocker.\n' +
          'Please allow pop-ups from this Web site.');
        }
      };
      this.setMimeType = function(mimeType) {
        this.mimeType = mimeType;
      };
      this.setHandlerResp = function(funcRef) {
        this.handleResp = funcRef;
      };
      this.setHandlerErr = function(funcRef) {
        this.handleErr = funcRef;
      };
      this.setHandlerBoth = function(funcRef) {
        this.handleResp = funcRef;
        this.handleErr = funcRef;
      };
      this.setRequestHeader = function(headerName, headerValue) {
        this.headers.push(headerName + ': ' + headerValue);
      };
    
    }
    Along with messing around with a ajax / php login script.

    Code:
    // Record Locking
    
    var login = new function() {
        
        this.ajax = new Ajax();
        this.target_url = 'modules/base/session.php?id=1';
        this.login_result = null;
        this.login_response = null;  
        
        this.init = function() {
            var self = login;
            document.forms['form1'].onsubmit = function() { return false; }
            exec = document.forms['form1'].elements['command']
            exec.onclick = self.process_login;
        }
        
        this.process_login = function() {
        
            var username = document.forms['form1'].elements['name'].value;
            var password = document.forms['form1'].elements['password'].value;
    
            var self = login;
            self.ajax.doPost(self.target_url, "data=" + username + "," + password + "", self.login_response);    
        }  
        
        this.login_response = function(str) {
        
            var self = login;
            
            var split_array = str.split(',');
            self.login_result = split_array[0].toLowerCase();
            self.login_response = split_array[1];
            
            result_div = document.getElementById('ajax');
            result_div.className = "bodyline";
            result_div.setAttribute("style", "width:250px;margin-top:20px;padding:0px;");
            result_div.innerHTML = "<h2 style='text-transform:none;margin:4px;'><div class=tiny style='padding:0px;'>" + self.login_response + "</div></h2>";
            
            document.forms['form1'].elements['name'].value = '';
            document.forms['form1'].elements['password'].value = '';     
            
                if (self.login_result == 1) {
                    document.location = 'modules/base/snapshot.php';
                }
        }
          
    }
    
    window.onload = login.init;
    Funnily enough the script works, but if you enter the wrong username and password once, when the script next runs you get (if you click submit again) you get this error (refer attachment).

    But everything still runs. Does anyone have any ideas how to clear this up?
    Attached Images Attached Images

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    looking at the following code, where does login get defined?

    Code javascript:
    this.process_login = function() {
     
        var username = document.forms['form1'].elements['name'].value;
        var password = document.forms['form1'].elements['password'].value;
     
    var self = login;
        self.ajax.doPost(self.target_url, "data=" + username + "," + password + "", self.login_response);    
    }

    Try:

    Code javascript:
    this.process_login = function() {
     
        var username = document.forms['form1'].elements['name'].value;
        var password = document.forms['form1'].elements['password'].value;
     
    var self = this;
        self.ajax.doPost(self.target_url, "data=" + username + "," + password + "", self.login_response);    
    }



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
  •