Javascript Tianium-从Web服务器检索JSON时出错
我是钛合金新手,我正在尝试从PHP/MySQL文件中检索JSON数据,该文件在我的浏览器上正确显示JSON,如下所示:Javascript Tianium-从Web服务器检索JSON时出错,javascript,json,titanium,Javascript,Json,Titanium,我是钛合金新手,我正在尝试从PHP/MySQL文件中检索JSON数据,该文件在我的浏览器上正确显示JSON,如下所示: {"todo":[{"todo":"Some Sample Text"},{"todo":"Hello"}]} 下面我将使用Ti.Network.createHTTPClient方法检索JSON。 但是我得到了这个错误:uncaughtsyntaxerror:index.html处的输入意外结束 我不知道我做错了什么,因为我只是复制了教程代码。提前谢谢 以下是教程代码: in
{"todo":[{"todo":"Some Sample Text"},{"todo":"Hello"}]}
下面我将使用Ti.Network.createHTTPClient方法检索JSON。
但是我得到了这个错误:uncaughtsyntaxerror: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();
//存储todo列表中数据的数组
var dataArray=[];
//我们执行函数来显示第一个视图的数据
getToList();
函数getToList(){
//函数使用HTTP连接到web服务器并传输数据。
var sendit=Ti.Network.createHTTPClient({
onload:函数(e){
var json=json.parse(this.responseText);
var json=json.todo;
//如果数据库为空,则显示警报
如果(json.length==0){
$.tableView.headerTitle=“数据库行为空”;
}
//清空数据以刷新视图
dataArray=[];
//将JSON数据插入表视图
对于(var i=0;i根据您最后的评论,问题与钛无关。问题在于服务器的配置,因此您必须查看该问题
您可以在此处找到更多信息:
你能提供index.html
中的代码吗?顺便说一句,它应该是index.xml
-Alloy视图。当然,我现在就编辑我的帖子!但是,使用一个测试变量,比如var tableData=[{title:'Apples'},{title:'banana'}];
没有错误。因此我认为错误应该在http方法上,可能在websever的某些属性中,但我与Apache本地服务器和远程服务器有相同的行为。我不明白为什么错误消息包含index.html
。您真的使用了与上面相同的代码吗?这是来自Ti.API.debug(e.error)的错误吗
?好的,你有与相同的url吗http://127.0.0.1/test/read.php
?如果您正在向read.php
发送请求,那么我不知道为什么消息中会提到index.html
。请尝试console.log(this.responseText);
我已设置标题(“访问控制允许源代码:”);
在PHP文件上,但这还不够,因为控制台打印出这个不同的错误请求标头字段X-Titanium-Id不被访问控制允许标头所允许。
。可以找到我问题解决方案的最后一部分。感谢0101告诉我正确的方法;)
<?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();
<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>