Titanium Community Questions & Answer Archive

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

Cookies for AJAX Requests -- How do they work?

I have an application that I am attempting to develop for Titanium Desktop to run on Mac and PC. While the main "guts" of the application are currently stored locally (so under the app:// system), the application makes heavy use of AJAX API calls to an external site.

The problem is that the Titanium application doesn't seem to be storing and sending back the cookies that the AJAX API is attempting to set. The API uses PHP sessions, so in theory I really only need to get one cookie set (the session ID), and then the rest can really just be handled server-side.

What's the solution here? In Adobe AIR this "just works" the way that one would expect (the same way a browser would handle it, basically). If I can just overcome this one obstacle I think I should be able to really get rolling on this project, but for now I'm about read to pull my hair out.

— asked September 21st 2010 by Riley Dutton
  • cookies
  • desktop
0 Comments

2 Answers

  • Accepted Answer

    Titanium for Desktop is dead I think. Because they don't seem to reply to desktop based questions. Also, there are no updates to desktop. I was a convert from AIR to Ti but now back to AIR.

    I also faced the same problem but then ultimately I had to use database file.

    — answered September 22nd 2010 by Gaurav Chandra
    permalink
    1 Comment
    • Interesting. Thanks for the heads-up. I would have used AIR as well except for their sandbox restriction on not being able to use eval() after onLoad()…it really conflicted with a javascript library I was using (JSIO) that was vital to the project. Titanium Desktop doesn't seem to have any of the same restrictions.

      Following advice from another question, if you basically call Titanium.UI.createWindow("http://domain.com/somepagethatsetssessioncookie"), the session cookie will get set correctly, and then get sent correctly with the subsequent AJAX calls. Here's the exact code I used to do it, in case anyone else out there runs into the same problem (in Javascript, btw):

      //Make sure our session cookie gets set...
      var invisiwindow = Titanium.UI.createWindow("http://example.com/page");
      invisiwindow.visible = false;
      invisiwindow.open();
      invisiwindow.addEventListener(Titanium.PAGE_LOADED, function() {
              invisiwindow.close();
      });
      

      — commented September 22nd 2010 by Riley Dutton
  • I realize this post is old but for those still encountering this issue, have you tried specifying withCredentials = 'true'?

    According to Apple's Documentation, you must specify withCredentials = 'true' or cookies will not be sent and received.

    Example

    var req = new XMLHttpRequest();
    req.open('GET', 'http://www.apple.com');
    req.withCredentials = 'true';
    req.onreadystatechange = function() {
        if (req.readyState==4 && req.status==200) {
            Titanium.API.info(req.responseText);
        }
    }
    response = req.send();
    
    — answered December 5th 2011 by Matthew Hershberger
    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.