[英]TypeError: Cannot read property 'open' of undefined (Phonegap and Ionic)
我正在使用Phonegap Build和Ionic構建移動應用程序。 我正在嘗試為Web服務創建XMLHttpRequest以獲取一些XML數據。 Web服務需要3個參數。 我一直收到以下錯誤:
ionic.bundle.js:26794 TypeError:無法讀取undefined屬性'open'。
關於我可能做錯什么的任何想法?
我已經附上了下面的整個方法。
$scope.result = function callService(requestAction, requestXML){
var httpObj = undefined;
if(window.XMLHttpRequest){
httpObj = new XMLHttpRequest();
}
else if (window.ActiveXObject){
httpObj = new ActiveXObject("Microsoft.XMLHTTP");
}
if(httpObj = undefined){
alert("Failed creating Http Object");
return "";
}
if(requestAction == undefined || requestAction == null){
requestAction = "";
}
var async = false;
var url = "theurl";
var systemID = "thesystemid";
var requestAction = "GetEnquiry";
var requestXML = "therequestxml";
var params = "SystemID=" + systemID + "&RequestAction=" + requestAction + "&RequestXML=" + requestXML + "&OutputFormat=JSON";
var headers = "Content-type , application/x-www-form-urlencoded; charset=UTF-8";
httpObj.open("POST", url, async);
httpObj.send(params);
if(httpObj.readyState == 4 && httpObj.status == 200){
var result = httpObj.responseText;
console.log("This is the result: " + result);
return result;
}
else {
var result = httpObj.responseText;
return result;
}
};
})
問題是httpObj
對象未定義,您嘗試訪問undefined
的方法。 您沒有在警報中定義錯誤消息(當httpObj
undefined
)的原因是您的條件不正確。 這個
if(httpObj = undefined){
應該像下面這樣:
if(httpObj === undefined){
或者等同於:
if(httObj){
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.