Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    May 2009
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Help! Sending Request, yet no readystatechange..

    So I have a problem that I can't seem to fix, no matter what I do.
    The code below SHOULD create a 'slideObj' object and then use it to get some information from a php file called queryslides.php.

    However, the code seems to only get to sending the request; it never RECIEVES any data back.
    The function outlined in "this.request.onreadystatechange = this.infoReturn;" which is infoReturn() never gets called up. Everything before it works great, but it stops after the onreadystatechange part.

    I have my code below, with comments inserted for ease of reading.
    Code:
    function slideObj() {
    this.request = null;         //get a request object
       try {
         this.request = new XMLHttpRequest();
       } catch (trymicrosoft) {
         try {
           this.request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (othermicrosoft) {
           try {
             this.request = new ActiveXObject("Microsoft.XMLHTTP");
           } catch (failed) {
             this.request = null;
           }
         }
       }
       
        if (this.request === null) {
         alert("Error creating request object!");  
    }
    
    
    
       function sendGetServer() {
           this.request.open("GET", "queryslides.php", true); 
     
    //queryslides.php is a php file with only one active line
    //<?php
    //echo "1|r|d~3|s|f";
    //?>
    
         this.request.onreadystatechange = this.infoReturn;
         this.request.send(null);
         alert('Message Requested');  
        }
    
    
    //HERE IS THE PROBLEM *****
    //Every 'alert' is activated, except for those in the two functions below 
    
       function infoReturn() {
         if (this.request.readyState == 4) {
           /* Get the response from the server */
           var info = this.request.responseText;
    	alert('Info recieved');
           /* Format Data */
           this.goOn(info);
          }
    
       }
    
    
    //The next function works by itself, so this isn't really a problem
    //
    // Except for the alert line, you can just pretend the goOn() doesn't do anything.
    
    		function goOn(inf) {
    			
    			alert('working?');
    			
    		}
    	
    this.goOn = goOn;
    this.infoReturn = infoReturn;
    this.sendGetServer = sendGetServer;
    }
    
    var hey = new slideObj();
    hey.sendGetServer();
    alert('end');
    What can I do?
    I have a link to the page here: http://hiroshifukada.no-ip.biz/sense/TEST.html~ (Note: The linked file is not EXACTLY the same as the example I gave. I'll update the file to what I show here as soon as I can.)

  • #2
    New to the CF scene
    Join Date
    May 2009
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation

    Also, when I removed the 'object-ness' of it-- the same code, but without the 'this.request's or the new object part. I just ran the code straight-out-- it worked okay.

    How would making the code in an object make it suddenly not work?
    How can I fix it?

  • #3
    New to the CF scene
    Join Date
    May 2009
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb Heelllllpppppppp!!!

    I've figured out one more thing:
    infoReturn() IS being activated, however, I have isolated the if-statement as the problem.
    Code:
    function infoReturn() {
    alert('This alert is being read, 4 times. 1 for every readyState change, I presume');
         if (this.request.readyState == 4){
    //HERE is where it won't work
    }
    I've set the if statement to (this.request.readyState == 4), (this.request.readyState), and (this.request.readyState != 4). NONE of these seem to work.
    Please, WHY doesn't this if-statement WORK??!

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    http://www.codingforums.com/showpost...82&postcount=1

    see 1.2:

    http://www.codingforums.com/rules.htm

    ask one of the moderators to remove one.

    best regards

  • #5
    New to the CF scene
    Join Date
    May 2009
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Any ideas on my problem though?

  • #6
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Code:
    this.request.onreadystatechange = this.infoReturn;
    needs to be

    Code:
    var that = this;
    this.request.onreadystatechange = function(){that.infoReturn()};
    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #7
    New to the CF scene
    Join Date
    May 2009
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    Wow...
    Thank you so much! It finally works!

    But still... WHY does it work?
    Maybe it's just because I'm fairly new to AJAX, but I don't see why this new code works when the old one fails..

  • #8
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Learn about closures and scope.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]


  •  

    Tags for this Thread

    Posting Permissions

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