【问题标题】:Titanium - Error while retrieving JSON from webserverTitanium - 从网络服务器检索 JSON 时出错
【发布时间】:2014-06-21 17:44:25
【问题描述】:

我是 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>

【问题讨论】:

  • 你能提供index.html里面的代码吗?顺便说一句,应该是index.xml - Alloy View。
  • 当然,我现在正在编辑我的帖子!
  • 但是,使用像 var tableData = [ {title: 'Apples'}, {title: 'Bananas'}]; 这样的测试变量没有错误。所以我认为错误应该在 http 方法上,可能在 websever 的某些属性中,但我对 Apache 本地服务器和远程服务器有相同的行为。
  • 我不明白为什么错误消息包含index.html。你真的使用与上面相同的代码吗?这是来自Ti.API.debug(e.error); 的错误吗?
  • 好的,你有没有和http://127.0.0.1/test/read.php一样的url?如果您向read.php 发送请求,那么我不知道为什么该消息会提到index.html。试试console.log(this.responseText);

标签: javascript json titanium


【解决方案1】:

根据您的最后评论,该问题与钛无关。问题出在你的服务器配置中,所以你必须看看...

你可以在这里找到更多:

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

【讨论】:

  • 我在 PHP 文件上设置了header("Access-Control-Allow-Origin: *");,但这还不够,因为控制台会打印出这个不同的错误Request header field X-Titanium-Id is not allowed by Access-Control-Allow-Headers.。我的问题解决方案的最后一部分可以找到there。感谢 0101 告诉我正确的方法;)
猜你喜欢
  • 1970-01-01
  • 2021-02-02
  • 1970-01-01
  • 1970-01-01
  • 2013-04-29
  • 1970-01-01
  • 1970-01-01
  • 2011-07-30
  • 1970-01-01
相关资源
最近更新 更多