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 :-)
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!
-
Cheers mate!
-
Cheers, Thanks for making it simple!
-
Steven,
I think there is an mistake in your sample code.The line
Ti.API.info('FirstName=' + jsonObject.contact[1].firstName);
should be like this
Ti.API.info('FirstName=' + jsonObject.contactInfo[1].firstName);
-
thanks, glad it helped. Just doing my part to help balance out my many takes without a lot of give backs. :)
-
how can array these to a table row?
-
it should reference contactInfo not 'contact' in the json object
-
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 ?
-
THANK YOU .
you saved me