You must Login before you can answer or rate any questions.

Using AJAX in Titanium

0

I am getting confused with how the AJAX part of the Titanium API works, and what code we are still required to use. jQuery in the past has taken a lot of this load off my shoulders, so am curious what needs adding.

I basically have a php file on a remote server, and want to pasa variable in, and run an if statement, then pull a variable out into the mobile (android) app.

I have seen lots of code some use Titanium.ajax or xhr and createHTTPClient, and I am just confused as to what I should be using to get my data. If you could help that'd be great.

5 Answers

0

Ok incase someone stumbles across this, here are some pointers. In php I used json_encode(). As for decoding it, the kitchen sink example I needed was xhr_utf8.js. The results were then used in the same object way in javascript varName['objectLevel1']['objectLevel2']

One thing to note though is to check your emulator is connected to the enternet, I had a ubg in mine, meaning I had to open an emu BEFORE titanium opened one.

Hope this helps others


0

No, I wasn't using XML, I was trying to return using JSON format, so used:

$encoded = json_encode($arr);
Is this not possible? I couldn't find a JSON example, so used the twitter one as a starting block.


0

I'm no XML expert, but your results string is not an XML document (unless you left all that out), with header and tags, etc.; perhaps the XML parser is barfing on the string? The Kitchen Sink example returns an entire XML doc from Twitter (see the info output on the simulator). You might try printing just this.responseText. Also, I think I had some problem with viewing/using the response text until I assigned it to a var first: var rt = this.responseText; Ti.API.info('rt=' + rt);

I got our PHP XHR code to work fine with our app, but I was not using the responseXML part.


0

Thanks for that, I got the twitter one working just fine, but when I tried to get xhr.open to open my own php file it seems xhr.onload doesn't get run (I amtesting with lots of API.info() calls).

All that my php file is outputting is:

["test0","test1","test2","test3"]
What I have is something like:
function getMapData(){
    var xhr = Titanium.Network.createHTTPClient();
    Titanium.API.info('yo2');
    xhr.onload = function()
    {
        Titanium.API.info('API.INFO');
        Ti.API.info('xml ' + this.responseXML + ' text ' + this.responseText);
    };
    Titanium.API.info('yo3');
    // open the client
    xhr.open('GET','http://www.xxx.com/xxx.php');
    Titanium.API.info('yo4');
 
    // send the data
    xhr.send();
    Titanium.API.info('yo5');
}
All of the yo's run, but nothing else, and not sure why


0

Hey Dan..

We have a custom XHR that follows the same API the standard XHR does on the web (with a few extra features like binary / multi-part support). You can find quite a few examples of our XHR in KitchenSink