gpt4 book ai didi

javascript - 类型错误 : Cannot read property 'open' of undefined (Phonegap and Ionic)

转载 作者:行者123 更新时间:2023-11-28 05:38:28 25 4
gpt4 key购买 nike

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

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

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

我在下面附上了完整的方法。

$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){

关于javascript - 类型错误 : Cannot read property 'open' of undefined (Phonegap and Ionic),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39154069/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com