Titanium Community Questions & Answer Archive

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

HTTP basic auth issue with JQuery $.ajax() in Titanium

Hey guys,

I'm writing a desktop app, using JQeury 1.4.2. I'm using the $.ajax() utility function to pull data, and passing in basic auth credentials. Authentication works fine when I run the app on Titanium Developer 1.2.1 on Mac OS X, but when I run the exact same code on the Linux version of Titanium Developer the app pops up a dialog box prompting for basic auth credentials. On Windows Vista, the call seems
to fail completely (couldn't find any useful log messages as to why). Is there a known issue with Titanium Developer and JQuery $.ajax() calls?

Code snippet:

     $.ajax({ url: this.apiroot + method,
         username: this.username,
         password: this.password,
         success: function(data, status, xhr) {
             onSuccess(status, data);
         },
         error: function(xhr, status, thrown) {
             onError(status, thrown);
         }
    });
— asked April 21st 2010 by Zach Copley
  • ajax
  • desktop
  • jquery
0 Comments

4 Answers

  • Accepted Answer

    Hey Zach

    It's possible the Webkit / CURL (Windows) / libsoup (Linux) backends don't deal with HTTP Auth from XHR yet. You might try using our custom XHR implementation that should support username/password auth on all 3 platforms. The API is the same as the standard XHR but you use our API to construct it. Here's a rough equivalent of what you're trying to do (this might require tweaking):

    var xhr = Titanium.Network.createHTTPClient();
    xhr.onload = function() {
      if (this.status == 200) { onSuccess(this.status, this.responseText); }
      else { onError(this.status); }
    }
    xhr.open("GET", this.apiroot+method, true, this.username, this.password);
    xhr.send();
    
    — answered April 22nd 2010 by Marshall Culpepper
    permalink
    1 Comment
    • awesome answer

      — commented December 12th 2011 by Rahul Pandey
  • Hey Marshall,

    Thanks for your answer! That seems like it should do the trick, but unfortunately I cannot get xhr.onload to fire! I know the GET request is getting made correctly and returning a 200 status, because I can see it in my web server logs. I get lots of messages about Titanium.Network.HTTPClient readyState changing from 1 to 2 to 3 to 4, and things like that, but nothing happens.

    For debugging sake I made a super simple test app and the same thing happens. Here's the code:

    window.onload = function() {
    
        Titanium.API.debug("Window ready");
    
        var xhr = Titanium.Network.createHTTPClient();
    
        xhr.onload = function() {
            Titanium.API.info('HTTP status = ' + this.status);
            Titanium.API.info('Notice xml ' + this.responseXML + ' text ' + this.responseText);
        };
    
        xhr.open('GET', 'http://identi.ca/api/statuses/show/29560058.xml');
    
        xhr.send();
    
    }
    

    And I get the following in the logs (xhr.onload doesn't seem to get called):

    ....
    [04:45:06:478] [Titanium.NetworkStatus] [Debug] ti.Network: Online status changed ==> 1
    [04:45:06:642] [Titanium.JavaScript.KJSUtil] [Debug] Evaluating JavaScript file at: /Library/Application Support/Titanium/modules/osx/tiui/1.0.0/ui.js
    [04:45:06:647] [Titanium.Network.HTTPClient] [Debug] Changing readyState from 0 to 1 for url:https://api.appcelerator.net/p/v1/app-track
    [04:45:06:648] [Titanium.Proxy] [Debug] Looking up proxy information for: https://api.appcelerator.net/p/v1/app-track
    [04:45:06:649] [Titanium.Proxy] [Debug] Proxy bypass entry: *.local
    [04:45:06:650] [Titanium.Proxy] [Debug] Proxy bypass entry: 169.254/16
    [04:45:06:650] [Titanium.Proxy] [Debug] Checking whether https://api.appcelerator.net/p/v1/app-track should be bypassed.
    [04:45:06:650] [Titanium.Proxy] [Debug] bypass entry: scheme='' host='.local' port='0'
    [04:45:06:650] [Titanium.Proxy] [Debug] bypass entry: scheme='' host='169.254/16' port='0'
    [04:45:06:650] [Titanium.Proxy] [Debug] No bypass
    [04:45:06:651] [Titanium.Proxy] [Debug] Using direct connection.
    [04:45:06:728] [Titanium.API] [Debug] Window ready
    [04:45:06:728] [Titanium.Network.HTTPClient] [Debug] Changing readyState from 0 to 1 for url:http://identi.ca/api/statuses/show/29560058.xml
    [04:45:06:729] [Titanium.Proxy] [Debug] Looking up proxy information for: http://identi.ca/api/statuses/show/29560058.xml
    [04:45:06:729] [Titanium.Proxy] [Debug] Checking whether http://identi.ca/api/statuses/show/29560058.xml should be bypassed.
    [04:45:06:729] [Titanium.Proxy] [Debug] bypass entry: scheme='' host='.local' port='0'
    [04:45:06:729] [Titanium.Proxy] [Debug] bypass entry: scheme='' host='169.254/16' port='0'
    [04:45:06:729] [Titanium.Proxy] [Debug] Using direct connection.
    [04:45:07:456] [Titanium.Network.HTTPClient] [Debug] Changing readyState from 1 to 2 for url:http://identi.ca/api/statuses/show/29560058.xml
    [04:45:07:456] [Titanium.Network.HTTPClient] [Debug] Changing readyState from 2 to 3 for url:http://identi.ca/api/statuses/show/29560058.xml
    [04:45:07:457] [Titanium.Network.HTTPClient] [Debug] Changing readyState from 3 to 4 for url:http://identi.ca/api/statuses/show/29560058.xml
    [04:45:08:338] [Titanium.Network.HTTPClient] [Debug] Changing readyState from 1 to 2 for url:https://api.appcelerator.net/p/v1/app-track
    [04:45:08:338] [Titanium.Network.HTTPClient] [Debug] Changing readyState from 2 to 3 for url:https://api.appcelerator.net/p/v1/app-track
    [04:45:08:338] [Titanium.Network.HTTPClient] [Debug] Changing readyState from 3 to 4 for url:https://api.appcelerator.net/p/v1/app-track
    ....
    

    What am I doing wrong?

    Thanks,

    Zach

    — answered April 23rd 2010 by Zach Copley
    permalink
    0 Comments
  • Okay, looks like there is a bug. You have to have a function defined for Titanium.Network.HTTPClient.onreadystatechange or onload wont fire. If you just define an empty function for onreadystatechange the HTTPclient works.

    — answered April 23rd 2010 by Zach Copley
    permalink
    0 Comments
  • What about the reported Httpclient memory leaks? Is it safe to use it? If not, how can you do a Socket write?

    — answered November 21st 2011 by adrian rosian
    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.