我用钛制作了一小段代码来从网页上获取数据。但是当我运行应用程序并按下将触发该功能的按钮时,它不会显示数据。
有人可以向我解释我做错了什么,为什么我做错了?
这是我的代码:
// include needed files
Ti.include('responsive.js');
//Ti.include('http_connection.js');
//Create the screen
//The home screen
var homeWindow = Ti.UI.createWindow({
exitOnClose: true,
fullscreen: false,
title: 'Advanced'
});
var homeView = Ti.UI.createView({
backgroundColor: 'white'
});
var homeLabel = Ti.UI.createLabel({
top: 20,
left: 30,
height: 30,
text: 'Test text',
color: 'black',
font: {fontSize: 18}
});
var testButton1 = Ti.UI.createButton({
title: 'test',
backgroundColor: 'red',
top: 55,
left: per10,
width:per60,
height: 30,
color: 'black',
font: {fontSize: 14}
});
var testButton2 = Ti.UI.createButton({
title: 'test2',
backgroundColor: 'blue',
top: 95,
left: per10,
width:per60,
height: 30,
color: 'black',
font: {fontSize: 14}
});
testButton2.addEventListener('click',function(e){
Ti.API.info("Button Clicked");
http_con();
Ti.API.info("Button Clicked 2");
//alert('test');
});
function http_con() {
Ti.API.info('hya');
//Database connection
var http_client = Ti.Network.createHTTPClient();
http_client.open('POST', 'http://rdbomers-hp:89/ceres');
//If variables has been send
http_client.onload = function() {
Ti.API.info('subjects: ' + this.responseText);
callback(this.responseText);
};
//If there is an error
http_client.onerror = function(e) {
Ti.API.info('error: ' + JSON.stringify(e));
};
};
//Creating the application
//Home screen
homeWindow.add(homeView);
homeView.add(homeLabel);
homeView.add(testButton1);
homeView.add(testButton2);
homeWindow.open();
我已将thi Ti.API.info('hya')
放入函数中以检查它是否到达,它显示HYA,但我希望它显示网页的内容。
答案 0 :(得分:0)
您应该在调用.open
之前定义回调 - 就像这样
function http_con() {
Ti.API.info('hya');
//Database connection
var http_client = Ti.Network.createHTTPClient();
//If variables has been send
http_client.onload = function() {
Ti.API.info('subjects: ' + this.responseText);
callback(this.responseText);
};
//If there is an error
http_client.onerror = function(e) {
Ti.API.info('error: ' + JSON.stringify(e));
};
http_client.open('POST', 'http://rdbomers-hp:89/ceres');
};