2012-02-23 58 views
0

我正在使用下面的代码来拉取数据,什么也没有发生。这是我的PHP代码。钛Appcelerator - 如何拉取远程数据

$getall = "SELECT * FROM pages WHERE account_id=$id ORDER BY course_id";   
$showall = @mysqli_query ($dbc, $getall); // Run the query. 

$json = array(); 

if (mysqli_num_rows($showall) > 0) 
{ 

while ($row=$showall->fetch_assoc()) {  

     $json[]=array( 
    'logged' => true, 
     'pagename'=>$row['pagename'], 
     ); 

     } // end while 


header("Content-Type: text/json"); 
echo json_encode(array('pages' => $json)); 


} 

而这里是我的JS代码运行的应用程序。

sendit.open('GET', 'http://myurl.com/mypages.php'); 
sendit.send(); 
sendit.onload = function(){ 
    var json = JSON.parse(this.responseText); 
    var json = json.pages; 
    var dataArray = []; 
    var pos;  

    for(pos=0; pos < json.length; pos++){ 

     dataArray.push({title:'' + json[pos].pagename + ''}); 
     // set the array to the tableView 
     tableview.setData(dataArray); 
    }; 

};  

var tableview = Ti.UI.createTableView({ 
}); 

currentWin.add(tableview); 

当我运行的应用程序,我得到的只是一张空白表。任何帮助将不胜感激。

+0

检查你的json数组长度或过去的样品JSON字符串 – Triode 2012-02-23 18:42:14

回答

0

要嵌入您在另一个数组的数组,所以在JavaScript端它会是:

dataArray.push({title:'' + json.pages[pos].pagename + ''}); 
            ^^^^^ - missing level 
+0

我想这...它并没有做出改变。 – 2012-02-23 15:12:10

0

你应该sendit.onload之前发送it.send。

我也建议你实现sendit.onerror方法,以及中间Ti.API.debug(东西); 你也使双声明的JSON,与工作,但不是很好。

+0

我改变了顺序,它没有工作。另外,我在脚本中使用了onerror方法,它不会给我一个错误。 – 2012-02-23 15:11:55

+0

你有没有像wireshark这样的嗅探器?你能够进入调试模式吗? – 2012-02-23 15:15:01

+0

尝试: 'dataArray.push(Ti.UI.createTableViewRow({title:''+ json [pos] .pagename +''}));' – 2012-02-23 15:17:20

1

您是否试图将sendit.send();移动到sendit.open以下?

在你的onload事件中放入一个日志,以确保它被触发。

它指出在你为了即使进行注册调用open之前在onload必须定义docs