Jeronisha Jeronisha - 3 months ago 22
AngularJS Question

TypeError: Cannot read property 'open' of undefined (Phonegap and Ionic)

I'm building a mobile app using Phonegap Build and Ionic. I am trying to make an XMLHttpRequest to a web service to get some XML data. The web service requires 3 parameters. I keep getting the following error:


ionic.bundle.js:26794 TypeError: Cannot read property 'open' of
undefined.


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;
}
};
})

Answer

The problem is the httpObj object which is undefined and you try to access a method on 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. This

if(httpObj = undefined){

should had been like below:

if(httpObj === undefined){