/ Published in: JavaScript
URL: http://www.appcelerator.com/products/titanium-mobile/
Appcelerator Titanium Mobile is a framework + IDE designed to help you build iPhone applications using only HTML and Javascript. This example shows how to make a simple Ajax request
Expand |
Embed | Plain Text
<html> <head> <title>Find City By ZIP Code</title> <script type="text/javascript" src="jquery-1.3.2.js"></script> <style type="text/css" media="screen"> #textField{ width:100px; height:30px } #search-btn{ width:100px; height:47px; color:#0000ff; } #response-txt{ width:300px; text-align:center; font-family:helvetica, impact, sans-serif; font-weight: bold; } .center { margin-right:auto;margin-left:auto;width:320px; } </style> <script type="text/javascript" charset="utf-8"> var xhr; var xhrResponse; var alert; var xmlCity; var xmlState; var zipCode; var tf; var searchBtn; function initialize(e){ /**************************** Create Ajax Request ****************************/ xhr = Titanium.Network.createHTTPClient(); xhrResponse = $("#response-txt"); /**************************** Create TextField ****************************/ tf = Titanium.UI.createTextField({ id:'textField', value:'90026', color:'#336699', backgroundColor:'#eeeeee', returnKeyType:Titanium.UI.RETURNKEY_DONE, enableReturnKey:true, keyboardType:Titanium.UI.KEYBOARD, autocorrect:false, hintText:'Enter ZIP', textAlign:'left', clearOnEdit:true, borderStyle:Titanium.UI.INPUT_BORDERSTYLE_BEZEL, clearButtonMode:Titanium.UI.INPUT_BUTTONMODE_NEVER, }); $(tf).bind("change", onTfChangeHandler ); function onTfChangeHandler(e){ tf.value = e.value; } /**************************** Create Search Button ****************************/ searchBtn = Titanium.UI.createButton({ id:'search-btn', title:'Search' }); $(searchBtn).bind("click", onSearchClickHandler ); function onSearchClickHandler(e){ constructAndSend(); } /**************************** Ajax onLoad Request ****************************/ xhr.onload = function() { Titanium.API.info( "xhr.onload: " ); var xml = this.responseXML; // xml now is a Javascript DOM object you can use var xmlDoc = xml.documentElement; try { // get a value of the first tag myelement found in XML doc xmlCity = xmlDoc.getElementsByTagName("CITY")[0].childNodes[0].nodeValue.toString(); xmlState = xmlDoc.getElementsByTagName("STATE")[0].childNodes[0].nodeValue.toString(); } catch(err) { $(xhrResponse).html = ""; constructAlert( "Zip Code Not Found. "); } //xhrResponse.innerHTML = zipCode + "<br>is the ZIP Code for<br>" + xmlCity + ", " + xmlState; $(xhrResponse).html( zipCode + "<br>is the ZIP Code for<br>" + xmlCity + ", " + xmlState ); }; } function constructAndSend(){ //Capture the Zip Code zipCode = $(tf).val(); Titanium.API.info( "constructAndSend: " + zipCode ); //Confirm that a proper Zip Code has been entered if ( zipCode != null && zipCode.length == 5 ){ //Fire Off the Ajax Request xhr.open("GET","http://www.webservicex.net/uszip.asmx/GetInfoByZIP?USZip=" + zipCode); xhr.send(null); } else { //Show any errors constructAlert( "A 5-digit ZIP Code must be entered." ); } } /**************************** Create Alert Message ****************************/ function constructAlert( message ){ var alert = Titanium.UI.createAlertDialog(); alert.setTitle("Error"); alert.setMessage( message ); alert.show(); return; } </script> <script type="text/javascript" charset="utf-8"> $(document).ready(function() { initialize(); }); </script> <body style="background-color:#999999"> <div id="textField" class="center"></div> <br/> <div id="search-btn" class="center"></div> <br/><br/> <div id="response-txt" class="center"></div> </body> </html>
Comments
Subscribe to comments
You need to login to post a comment.

I tried your code and text boxes cannot be displayed. May I know how to add textboxes with titanium api in html doc? Thanks.
Do you have jQuery installed?
I'm new to development with this tool, I've been looking hard for a small sample like this to work from. But it seems like all code I find does not compile. Can you give any advice? Note, I do have the latest version of titanium and followed all the instructions to get up and running and most importantly, kitchen sink works! jquery-1.3.2.js was included with kitchen sink, so I copied that file to my project resource folder. I'm not sure if I'm supposed to just copy paste your code snippet into "app.js" and it is supposed to work? I'm currently trying this with the andriod emulator (APIs 2.3.1), it installs and beings to run then the andriod emulator (not titanium) shows "syntax error" for line "#textField{ width:100px; height:30px }" (this is line 8 of your code snippet).
Any help would be greatly appreciated.
Thanks!