Loading TableView data from external file
Hello,
I've been given the task to create an app showing recently acquired products by a shop. This is my first iPhone app, so I'm not that familiar with the Titanium UI, I am however familiar with JavaScript.
Thanks to Titanium Appcelerator, this seems pretty easy. I have, however, one problem. At the moment, my product list is an array like the following:
var ProductArray = [];
ProductArray.push({
"id":ProductArray.length,
"name":"Product 1 Name",
"description":"Product 1 Description",
"image":"dev/gold_ring.png",
"date":"Product 1 Date"
});
ProductArray.push({
"id":ProductArray.length,
"name":"Product 2 Name",
"description":"Product 2 Description",
"image":"dev/silver_ring.png",
"date":"Product 2 Date"
});
Now, what I would like, is my array to be given from a PHP script on the web in some sort of JSON format - how can I do that?
I've tried using an XMLHttpRequest but without luck.
Any thoughts?
Caspar
3 Answers
-
Accepted Answer
var xhr = Ti.Network.createHTTPClient(); xhr.onerror = function(e) { alert(e.rrror) }; xhr.onload = function() { var data = JSON.parse(this.responseText); TI.API.info(data); }; xhr.open('POST', someremoteurl); xhr.send({var1:var1value,var2:var2value});
-
On the server side you have json_encode/json_decode functions ( php ). You can use this to return a valid json string .
With the reply you get ( needs to be a valid json string ) you can build custom rows for your table.
Here is a tutorial on how todo it.http://cssgallery.info/custom-row-for-tableview-in-appcelerator-titanium/
-
What I was looking for, was this: http://developer.appcelerator.com/question/28521/thought-i-would-share-some-code-remote-ajax-call-thats-more-jquery-like