Titanium Community Questions & Answer Archive

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

JSON http request working example here

I looked around and saw several questions about how to make this work, but no complete example for noob's like me. (or at least I didn't find one) So here is one:

sample.php

<?php
echo '{"contactInfo":[{"firstName":"john","lastName":"doe"},{"firstName":"sue","lastName":"smith"}]}';
?>

sample.js

var win = Titanium.UI.currentWindow;

var xhr = Titanium.Network.createHTTPClient();
xhr.onload = function() {
   var jsonObject = JSON.parse(this.responseText);

   // For debugging, if you want to see how the JSON parser see's the 
   // response text, un-comment the line below
   // Ti.API.info(jsonObject);

   // accessing a field
   Ti.API.info('FirstName=' + jsonObject.contact[1].firstName);
};

xhr.open("GET","http://localhost/sample.php");
xhr.send();
  • Testing Info -
    Platform: iPhone
    Ti: v1.4.2
    iSDK: v4.1

Output

FirstName=sue

hopefully noob enough :-)

— asked November 24th 2010 by Steven Day
  • http
  • json
  • php
0 Comments

9 Answers

  • Hi Steven!

    It`s a great example, to get it working I needed to do a small modification in the line: Ti.API.info('FirstName=' + jsonObject.contact[1].firstName);

    to: Ti.API.info('FirstName=' + jsonObject.contactInfo[1].firstName);

    This is for all the noobs that didn´t work for them the first time.

    Greetings!

    — answered January 12th 2012 by daniel axelrod
    permalink
    0 Comments
  • Cheers mate!

    — answered November 24th 2010 by Colton Arabsky
    permalink
    0 Comments
  • Cheers, Thanks for making it simple!

    — answered January 4th 2011 by Joe Irvine
    permalink
    0 Comments
  • Steven,
    I think there is an mistake in your sample code.

    The line
    Ti.API.info(&#39;FirstName=&#39; + jsonObject.contact[1].firstName);

    should be like this

    Ti.API.info(&#39;FirstName=&#39; + jsonObject.contactInfo[1].firstName);

    — answered May 23rd 2012 by Bishnu Pada Chanda
    permalink
    0 Comments
  • thanks, glad it helped. Just doing my part to help balance out my many takes without a lot of give backs. :)

    — answered January 5th 2011 by Steven Day
    permalink
    0 Comments
  • how can array these to a table row?

    — answered September 4th 2011 by Graham Jeffrey
    permalink
    0 Comments
  • it should reference contactInfo not 'contact' in the json object

    — answered July 10th 2012 by alex assouline
    permalink
    0 Comments
  • i don't understand how it works … can you explain briefly how titanium program interact with json web service … i have seen some exemples but i don't understood the steps of this interaction … in my opinion , Titinium program send a request object to json and json will return a response object … it works like this ?

    — answered April 9th 2014 by garali med karim
    permalink
    0 Comments
  • THANK YOU .
    you saved me

    — answered March 29th 2015 by jefferey ashitey
    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.