【问题标题】:TypeError: Cannot read property 'open' of undefined (Phonegap and Ionic)TypeError:无法读取未定义的属性“打开”(Phonegap 和 Ionic)
【发布时间】:2017-01-02 09:42:40
【问题描述】:

我正在使用 Phonegap Build 和 Ionic 构建一个移动应用程序。我正在尝试向 Web 服务发出 XMLHttpRequest 以获取一些 XML 数据。 Web 服务需要 3 个参数。我不断收到以下错误:

ionic.bundle.js:26794 TypeError: Cannot read property 'open' of 未定义。

关于我可能做错了什么有什么想法吗?

我在下面附上了整个方法。

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

【问题讨论】:

    标签: javascript angularjs cordova ionic-framework xmlhttprequest


    【解决方案1】:

    问题是httpObj 对象未定义,您尝试访问undefined 上的方法。您没有显示您在警报中定义的错误消息(当httpObjundefined)的原因是您的条件不正确。这个

    if(httpObj = undefined){
    

    应该是这样的:

    if(httpObj === undefined){
    

    或等同于:

    if(httObj){
    

    【讨论】:

    • 随便if (httpObj) {}
    猜你喜欢
    • 2015-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多