我剛學習JavaScript對象併產生問題。我有下面的代碼/ JS對象:帶有重置變量的JavaScript對象
更改:這是完整的代碼 - 這是一個Appcelerator的鈦項目!:
spike.xhr = spike.xhr || {};
spike.xhr.init = function() {
this.severResponse = '';
this.xhrMethod = 'GET';
}
spike.xhr.isOnline = function() {
if(Titanium.Network.online) {
return true;
} else {
return false;
}
}
spike.xhr.notOnline = function() {
var alertDialog = Titanium.UI.createAlertDialog({
title: CONFIG.PROJECT_SHORT,
message: 'You do not have an online connect. Please connect...',
buttonNames: ['Continue'],
cancel: 0
});
alertDialog.show();
}
spike.xhr.connect = function(url) {
if(spike.xhr.isOnline()) {
spike.xhr.clients = Ti.Network.createHTTPClient();
spike.xhr.clients.onload = function() {
if (spike.xhr.clients.status != 200 && spike.xhr.clients.status != 201) {
var alertDialog = Titanium.UI.createAlertDialog({
title: CONFIG.PROJECT_SHORT,
message: 'We are sorry, but we have received an status error!',
buttonNames: ['Continue'],
cancel: 0
});
alertDialog.show();
return false;
}
this.serverResponse = this.responseText;
}
spike.xhr.clients.onerror = function(e) {
Ti.API.log('ERROR: ' + e.status + ': ' + e.statusText + ' - ' + e.error);
}
spike.xhr.clients.open(this.xhrMethod, url);
spike.xhr.clients.send();
} else {
spike.xhr.notOnline();
}
}
spike.xhr.basicAuthentication = function(username, password) {
authstr = 'Basic ' +Titanium.Utils.base64encode(username+':'+ password);
spike.xhr.client.setRequestHeader('Authorization', authstr);
}
spike.xhr.setMethod = function(type) {
this.xhrMethod = type;
}
spike.xhr.response = function() {
return this.serverResponse;
}
如果我現在運行:
spike.xhr.connect('http://mydomain');
theResponse = spike.xhr.response();
我返回<null>
! - 爲什麼我沒有收到"This is a Test!"
回覆或更好的問題: 回到頂部"This is a Test!"
需要更改什麼?
XHR是一種XMLHttpRequest(AJAX)嗎? –
請提供'spike.xhr.connect'函數的代碼 - 可能你正在尋找返回值,而ajax調用仍在運行 – fcalderan
事情是,當我在this.serverResponse下面添加一個警報(this.serverRepsonse)時= 「這是一個測試!」,我正確地得到「這是一個測試!」?!?!? – codeworxx