TypeError: Cannot read property '未定义(Phonegap和Ionic)

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

本文关键字:未定义 Phonegap Ionic Cannot read property TypeError      更新时间:2023-09-26

我正在使用Phonegap Build和Ionic构建一个移动应用程序。我正试图使XMLHttpRequest到web服务以获得一些XML数据。web服务需要3个参数。我一直得到以下错误:

ionic.bundle.js:26794 TypeError:无法读取属性'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上的方法。不显示在警报中定义的错误消息(当httpObjundefined时)的原因是您的条件不正确。这个

if(httpObj = undefined){

应该像下面这样:

if(httpObj === undefined){

或类似的

if(httObj){