将对象值传递给for循环在Titanium中

时间:2014-03-04 08:11:51

标签: javascript for-loop titanium titanium-mobile

我是钛的新手,想要显示我的对象

的远程服务器数据
Object
 todo: Array[2]
  0: Object
   todo: "Khaleeq Raza"
   __proto__: Object
  1: Object
   todo: "Ateeq Raza"
  __proto__: Object
 length: 2
 __proto__: Array[0]
 __proto__: Object
  VM90:10

我的代码是用钛来创建行并想放置数据

    var client = new XMLHttpRequest();
    client.open("GET", "http://192.168.1.109/read_todo_list.php", true);
    client.send();
    client.onreadystatechange = function(){

    if (client.readyState==4 && client.status==200)
        {

            json = JSON.stringify(client.response);
        var get=JSON.parse(json);

        for( var i=0; i<get.length; i++){

        var row = Ti.UI.createTableViewRow({
            title: get[i].todo,
            hasChild : true,
            });     
            dataArray.push(row);                
        }

        $.tableView.setData(dataArray);

        }

    };

但我没有得到任何显示有什么问题请帮助

1 个答案:

答案 0 :(得分:1)

钛中没有XMLHttpRequest,这是获取数据的简单方法(假设服务器配置正确且无需验证)。

var client = Ti.Network.createHTTPClient({
     // function called when the response data is available
     onload : function(e) {
         Ti.API.info("Received json: " + this.responseText);
         // Parse JSON
         var object = JSON.parse(this.responseText);
         // Get the array
         var todo = object.todo;
         // Create table rows
         var dataArray = [];
         for( var i=0; i<todo.length; i++){
             var row = Ti.UI.createTableViewRow({
                 title: todo[i].todo,
                 hasChild : true,
              });     
              dataArray.push(row);                
         }
         $.tableView.setData(dataArray);

         alert('success');
     },
     // function called when an error occurs, including a timeout
     onerror : function(e) {
         Ti.API.debug(e.error);
         alert('error');
     },
     timeout : 5000  // in milliseconds
 });
 // Prepare the connection.
 client.open("GET","http://192.168.1.109/read_todo_list.php");
 // Send the request.
 client.send();