在ionic 2中,我使用下面的代碼通過API將文件(.pdf和.doc擴展名)上傳到服務器。但我無法調用resp.success == 1或無法使用任何全局變量之後調用任何函數。我得到錯誤,如屬性不存在類型xmlhttpeventtarget因爲我想導航用戶到成功提交文件的下一頁,我需要在成功內部調用一些函數。Ionic 2:xhr.onload後調用函數
xhr.open('POST', "myurl", true);
xhr.onload = function() {
if (xhr.status == 200) {
var resp = JSON.parse(xhr.response);
console.log('Server got: in IOS ', resp);
console.log('Server got: in IOS ', resp.message);
if(resp.success == 1)
{
console.log("THIS IS SUCCESS")
}
else{
alert(resp.message)
return
}
};
}
xhr.send(this.fd);
哪些屬性不存在?請提供完整的錯誤堆棧跟蹤。順便說一下,如果你使用Ionic 2,爲什麼不使用Angular 2的Http Client(https://angular.io/docs/ts/latest/guide/server-communication.html)來抽象實現客戶端 - 服務器通信通過http? –
嘿謝謝你的迴應,我可以通過聲明一個初始化爲這個前綴的變量來解決這個問題:var self = this;那麼我可以使用self.function_name和self.variable_name來使用所有函數和全局變量。 – Vasanth