我是一个 Titanium 新手,我正在尝试从 PHP/MySQL 文件中检索 JSON 数据,该文件在我的浏览器上正确显示如下 JSON:
{"todo":[{"todo":"Some Sample Text"},{"todo":"Hello"}]}
我正在关注This Tutorial使用方法 Ti.Network.createHTTPClient 检索 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>
最佳答案
根据您最后的评论,该问题与钛无关。问题出在您的服务器配置中,因此您必须查看...
您可以在这里找到更多信息:
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
关于javascript - Titanium - 从网络服务器检索 JSON 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24344223/