2012-02-23 62 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