我是Titanium新手,我正在尝试从PHP / MySQL文件中检索JSON数据,该文件在我的浏览器上正确显示了类似的JSON:
{"todo":[{"todo":"Some Sample Text"},{"todo":"Hello"}]}
我正在使用方法Ti.Network.createHTTPClient跟踪This Tutorial来检索JSON。
但是我收到此错误:Uncaught SyntaxError: Unexpected end of input at /index.html
我不知道我做错了什么,因为我只复制了教程代码。提前谢谢!
下面是教程代码: 的 index.js
//Array to store the data from the todo list
var dataArray = [];
//We execute the function to show the data for the first view
getTodoList();
function getTodoList () {
//function to use HTTP to connect to a web server and transfer the data.
var sendit = Ti.Network.createHTTPClient({
onload: function(e){
var json = JSON.parse(this.responseText);
var json = json.todo;
//if the database is empty show an alert
if(json.length == 0){
$.tableView.headerTitle = "The database row is empty";
}
//Emptying the data to refresh the view
dataArray = [];
//Insert the JSON data to the table view
for( var i=0; i<json.length; i++){
var row = Ti.UI.createTableViewRow({
title: json[i].todo,
hasChild : true,
});
dataArray.push(row);
};
$.tableView.setData(dataArray);
},
onerror: function(e){
Ti.API.debug(e.error);
alert('There was an error during the connection');
},
timeout:5000,
});
//Here you have to change it for your local ip
sendit.open('GET', 'http://127.0.0.1/test/read.php');
sendit.send();
};
$.mainTabGroup.open();
read.php
<?php
$username="root"; //------------your username usually root
$password="";//---------your password
$database="todolist";//----the name of the database
$mysqli = new mysqli("localhost",$username,$password,$database);
if (mysqli_connect_errno()) {
printf("Can't connect to SQL Server. Error Code %s\n", mysqli_connect_error($mysqli));
exit;
}
$json = array();
if($result = $mysqli->query("select todo from todolist.mylist")) {
while ($row=$result->fetch_assoc()) {
$json[]=array(
'todo'=>$row['todo'],
);
}
}
$result->close();
header("Content-Type: text/json");
echo json_encode(array( 'todo' => $json ));
$mysqli->close();
编辑,我的index.xml文件: 的 INDEX.XML
<Alloy>
<TabGroup id="mainTabGroup">
<!-- On click event execute getTodoList -->
<Tab id="tab1" onClick="getTodoList">
<Window id="readWin">
<TableView id="tableView"/>
</Window>
</Tab>
<Tab id="tab2">
<Window id="insertWin">
<View id="mainView">
<TextField id="inserTxtF"/>
<Button id="insertBtn" onClick="insertData" />
</View>
</Window>
</Tab>
</TabGroup>
</Alloy>
答案 0 :(得分:1)
根据您的上一条评论,该问题与Titanium无关。问题在于服务器的配置,因此您必须查看...
您可以在此处找到更多信息:
Failed to open remote server file in Titanium https://developer.appcelerator.com/question/148126/httpclient-post-data-mobile-web http://en.wikipedia.org/wiki/Cross-origin_resource_sharing