Titanium:当试图从远程服务器访问json数据时,给出了错误消息,但没有得到任何消息

Titanium:当试图从远程服务器访问json数据时,给出了错误消息,但没有得到任何消息,titanium,titanium-alloy,Titanium,Titanium Alloy,您好,我是钛移动应用程序的新手,我正在尝试从远程服务器获取数据,但失败。我正在使用钛工作室,构建:3.2.1.201402041146 这是我的index.xml文件 <Alloy> <TabGroup id="mainTabGroup"> <!-- On click event execute getTodoList --> <Tab id="tab1" onClick="getTodoList">

您好,我是钛移动应用程序的新手,我正在尝试从远程服务器获取数据,但失败。我正在使用钛工作室,构建:3.2.1.201402041146

这是我的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.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 () {

    var sendit = Ti.Network.createHTTPClient({
        onerror: function(e){
            Ti.API.debug(e.error);
            alert('There was an error during the conexion');
        },
        timeout:1000,
    });

    //Here you have to change it for your local ip

    sendit.open('GET', 'http://attarisoft.com/read_todo_list.php');  
    sendit.send(); 
    //Function to be called upon a successful response
    sendit.onload = function(){
        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);

     };
  }
//存储todo列表中数据的数组
var dataArray=[];
//我们执行函数来显示第一个视图的数据
getToList();
函数getToList(){
var sendit=Ti.Network.createHTTPClient({
onerror:函数(e){
Ti.API.debug(即错误);
警报(“连接过程中出现错误”);
},
超时:1000,
});
//在这里,您必须为您的本地ip更改它
sendit.open('GET','http://attarisoft.com/read_todo_list.php');  
sendit.send();
//成功响应时要调用的函数
sendit.onload=函数(){
var json=json.parse(this.responseText);
var json=json.todo;
//如果数据库为空,则显示警报
if(json.length==0){
$.tableView.headerTitle=“数据库行为空”;
}
//清空数据以刷新视图
dataArray=[];
//将JSON数据插入表视图
对于(var i=0;iindex.js

  function getTodoList(){
    var sendit = Ti.Network.createHTTPClient({

        onload : function(e){
          var json = JSON.parse(this.responseText);
          Ti.API.info(json);//Check your data to return

          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.info(e.error);
            alert('There was an error during the conexion');
        },
        timeout:5000, 
});

//Here you have to change it for your local ip

sendit.open('GET', 'http://attarisoft.com/read_todo_list.php');  
sendit.send();
}
$.mainTabGroup.open();
函数getToList(){ var sendit=Ti.Network.createHTTPClient({ onload:函数(e){ var json=json.parse(this.responseText); Ti.API.info(json);//检查要返回的数据 var json=json.todo; //如果数据库为空,则显示警报 if(json.length==0){ $.tableView.headerTitle=“数据库行为空”; } //清空数据以刷新视图 dataArray=[]; //将JSON数据插入表视图 对于(var i=0;i
关于选项卡的焦点

我认为您在这里遇到了一些问题:

  • 首先,在您的代码中,您在设置回调函数之前发送请求。虽然这应该可以工作,但不确定它的行为是否完全符合您的要求
  • 通过显式设置错误和超时(而不是在创建时分配这些值),我在处理这些请求时获得了更好的成功
尽管如此,请尝试以下代码:

function getTodoList () {

var sendit = Ti.Network.createHTTPClient();

//Here you have to change it for your local ip

sendit.open('GET', 'http://attarisoft.com/read_todo_list.php');  
//Function to be called upon a successful response
sendit.onload = function(){
    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);

 };
 sendit.onerror = function(){
        Ti.API.debug(e.error);
        alert('There was an error during the conexion');
 };
 sendit.setTimeout(10000); // 10 sec for timeout
 sendit.setRequestHeader("Content-Type", "application/json; charset=utf-8");
 sendit.send();
 }
函数getToList(){ var sendit=Ti.Network.createHTTPClient(); //在这里,您必须为您的本地ip更改它 sendit.open('GET','http://attarisoft.com/read_todo_list.php'); //成功响应时要调用的函数 sendit.onload=函数(){ var json=json.parse(this.responseText); var json=json.todo; //如果数据库为空,则显示警报 if(json.length==0){ $.tableView.headerTitle=“数据库行为空”; } //清空数据以刷新视图 dataArray=[]; //将JSON数据插入表视图 对于(var i=0;i固定如下

            var client = new XMLHttpRequest();
    client.open("GET", "http://www.domainname.com/read_todo_list.php", true);
    client.send();
    client.onreadystatechange = function(){

    if (client.readyState==4 && client.status==200)
        {
        //console.log(JSON.parse(client.response));
        //json = JSON.stringify(client.response);
        var get=JSON.parse(client.response);
        console.log(get['todo']);
        for( var i=0; i < get['todo'].length; i++){

        var row = Ti.UI.createTableViewRow({
            title: get['todo'][i].todo,
            hasChild : true,
            });     
            dataArray.push(row);                
        }

        $.tableView.setData(dataArray);

        }




    };
var-client=new-XMLHttpRequest();
client.open(“GET”http://www.domainname.com/read_todo_list.php“,对);
client.send();
client.onreadystatechange=函数(){
if(client.readyState==4&&client.status==200)
{
//log(JSON.parse(client.response));
//json=json.stringify(client.response);
var get=JSON.parse(client.response);
log(get['todo']);
for(var i=0;i
我可以看到,在网站中检索并显示的JSON中,inside.js尝试使用
Ti.API.info(JSON.stringify(this.responseText))
(类似于console.log)inside
sendit.onload=function()
post方法中还有一条警告消息,请尝试使用cURL直接发布到数据库,它应该类似于
cURL-X post-d@filenamehttp://hostname/resource
请让我知道这是否有帮助@谢谢你的回复,但我的问题没有解决。那么原因是什么呢?因为有这么多人面临这个问题m查看清单中的数据以从JSON获取。如果您试图封装问题,首先要检查与服务器的连接,
sendit.open('get'),'http://attarisoft.com/read_todo_list.php')
我不确定问题是否出在这行代码上,因为这直接导致sendit对象的onerror属性,你是在仿真器中还是在真实设备中测试它?它们都会显示日志错误,请让我看看应用程序的日志我正在浏览器的移动Web预览中测试它。我还给出了简单的jsot。tx文件类似但问题相同。你能用curl\u x Post-d编写一个示例代码吗?有一件事我正在使用Win 7 x32机器。所以这没问题?谢谢,但这不是一个解决方案。同样的问题。实际上我也在加载事件和单击事件中调用此函数。但问题相同。Tab没有单击事件。你应该使用onFocus事件加载。我使用了焦点事件,但出现了相同的问题谢谢@LucasA,但这是一条警告消息,在连接过程中出现了错误。有一件事我使用的是Win 7 x32机器。这样就可以了吗?我还提供了sendit.open('GET',');sendit.setRequestHeader(“访问控制允许源代码”,“*”);sendit.send();但给出错误请求的资源上不存在“Access Control Allow Origin”头。因此不允许访问源“”。我认为您的操作系统版本不是问题所在。您正在开发哪些平台?您是否在防火墙后面
            var client = new XMLHttpRequest();
    client.open("GET", "http://www.domainname.com/read_todo_list.php", true);
    client.send();
    client.onreadystatechange = function(){

    if (client.readyState==4 && client.status==200)
        {
        //console.log(JSON.parse(client.response));
        //json = JSON.stringify(client.response);
        var get=JSON.parse(client.response);
        console.log(get['todo']);
        for( var i=0; i < get['todo'].length; i++){

        var row = Ti.UI.createTableViewRow({
            title: get['todo'][i].todo,
            hasChild : true,
            });     
            dataArray.push(row);                
        }

        $.tableView.setData(dataArray);

        }




    };