SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with JSON/JS Object - accessing variables

    PHP Code:
    var AJAX = {
        
    req: new XMLHttpRequest(),
        
    doneFuncnull,
        
    processReqChange: function() {
            
    // only if req shows "loaded"
            
    if (this.req.readyState == 4) {
                
    // only if "OK"
                
    if (this.req.status == 200) {
                    
    // ...processing statements go here...
                    
    doneFunc(this.req.responseText);
                } else {
                    
    alert("There was a problem retrieving the XML data:\n" req.statusText);
                }
            }
        },
        
    get: function(url,func) {
            
    this.doneFunc func;
            
    this.req.onreadystatechange this.processReqChange;
            
    this.req.open("GET"urltrue);
            
    this.req.setRequestHeader('If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT');        
            
    this.req.send(url);
        }
    }

    function 
    showAlert(r) {
        
    alert("done!: " r);
    }

    AJAX.get("test.xml",showAlert); 
    When I run this, the function processReqChange can't find "req" - anyone know why? Thanks.
    Last edited by funkdaddy; Nov 2, 2007 at 06:42.

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your AJAX is an object, not a constructor function.

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I make it a constructor function (I think I'm doing this right):
    PHP Code:
    function AJAX() {
        
    this.req = new XMLHttpRequest();
        
    this.doneFunc null;
        
    this.processReqChange = function() {
            
    // only if req shows "loaded"
            
    if (this.req.readyState == 4) {
                
    // only if "OK"
                
    if (this.req.status == 200) {
                    
    // ...processing statements go here...
                    
    doneFunc(this.req.responseText);
                } else {
                    
    alert("There was a problem retrieving the XML data:\n" req.statusText);
                }
            }
        }
        
    this.get = function(url,func) {
            
    this.doneFunc func;
            
    this.req.onreadystatechange this.processReqChange;
            
    this.req.open("GET"urltrue);
            
    this.req.setRequestHeader('If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT');        
            
    this.req.send(url);
        }
    }

    function 
    showAlert(r) {
        
    alert("done!: " r);
    }

    var 
    test = new AJAX();

    test.get("test.xml",showAlert); 
    Then I get the error "this.req has no properties" when it reads this.req.readyState as "this" now is referencing the window? )According to an alert(this)). How can I get the readyState/status? Thanks for your help, AS you can tell, I'm still trying to grasp creating custom functions/objects.

  4. #4
    SitePoint Zealot xzyfer's Avatar
    Join Date
    Mar 2004
    Location
    oz
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    your problem is scoping.
    in the line:

    Code:
    this.processReqChange = function() {...}
    the 'this' keyword now references the anonymous function, not the AJAX() function, as i assume u intended.
    the work around to something like this, as i know it, is to attach '.bind(this)' to the end of the function.

    Code:
    this.preocessReqChange = function() {...}.bind(this)
    however, i believe this only works if you are using the prototype library (http://www.prototypejs.org), which would also make ur ajax request alot neater.

    you just have to find a way to access the parent function, java has the 'getParentNode()' method which you could attach to the this operator, but i dont know if that will work in this sense, as i think its generally used to access parent elements in html tags.

    do some research into javascript scoping and ull be fine.


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
  •