[英]TypeError: Cannot read property 'open' of undefined (Phonegap and Ionic)
I'm building a mobile app using Phonegap Build and Ionic. 我正在使用Phonegap Build和Ionic构建移动应用程序。 I am trying to make an XMLHttpRequest to a web service to get some XML data. 我正在尝试为Web服务创建XMLHttpRequest以获取一些XML数据。 The web service requires 3 parameters. Web服务需要3个参数。 I keep getting the following error: 我一直收到以下错误:
ionic.bundle.js:26794 TypeError: Cannot read property 'open' of undefined. ionic.bundle.js:26794 TypeError:无法读取undefined属性'open'。
Any ideas as to what I might be doing wrong? 关于我可能做错什么的任何想法?
I have attached the entire method below. 我已经附上了下面的整个方法。
$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;
}
};
})
The problem is the httpObj
object which is undefined and you try to access a method on undefined
. 问题是httpObj
对象未定义,您尝试访问undefined
的方法。 The reason why you don't show the error message (when httpObj
is undefined
) you have defined in the alert is that you condition is not correct. 您没有在警报中定义错误消息(当httpObj
undefined
)的原因是您的条件不正确。 This 这个
if(httpObj = undefined){
should had been like below: 应该像下面这样:
if(httpObj === undefined){
or equivalently like: 或者等同于:
if(httObj){
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.