Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

httpclient not firing onload and readystateevents..

Hi Folks,
I'm building an app with extjs / jquery and trying to use the HTTPClient supplied by titanium.

The request executes and I can see the response text when I examine the object, but it does not fire the onload and onreadystatechange events..any ideas?

CODE

    var xhr = Titanium.Network.createHTTPClient();
    xhr.timeout = 1000000;    
    xhr.open("GET","http://www.cnn.com");


    xhr.onreadystatechange = function(){
        console.log("herer");
    } 
    xhr.onload = function() {
        try{
            console.log("herer1");
            alert(this.responseText);
        }catch(e){
            console.log("herer2");
            alert(e);
        }
    };


    xhr.send();       
— asked June 8th 2010 by K Ram
  • httpclient onload events
0 Comments

1 Answer

  • I'm assuming we're looking at Titanium Desktop here, in which case you might want to consider just using the jQuery or ExtJS ajax implementations. You may find them a little easier to work with.

    Using the HTTP client, it looks like you might be running into some timing issues - try calling open after assigning the event handlers - I dropped this code into the sandbox and it worked:

    <script>
    var xhr = Titanium.Network.createHTTPClient();
        xhr.timeout = 1000000;  
    
        xhr.onreadystatechange = function(){
            alert("herer");
        } 
        xhr.onload = function() {
            try{
                alert(this.responseText);
            }catch(e){
                alert(e);
            }
        };
    
         xhr.open("GET","http://www.cnn.com");
    
        xhr.send();
    </script>
    
    — answered June 8th 2010 by Kevin Whinnery
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.