我有两个js文件。一个是route.js
,第二个是xml.js
。在route.js
文件中,我正在调用xml.js
中的函数。该函数的作用是通过xml链接解析并返回xml对象。当我在xml.js中执行console.log($(xml))
时,它会显示xml,但是当我返回此对象并在console.log(Xml.init(param, param, param))
内执行route.js
时,它会返回undefined
。为什么?
这是我的代码
route.js
var Route = {
fromurl : null,
tourl : null,
from : null,
to : null,
init: function (fromaddress, toaddress) {
from = fromaddress;
to = toaddress;
fromurl = 'http://example.com/url=' + fromurl;
tourl = 'http://example.com/url=' + tourl;
Route.searchRoute();
},
searchRoute: function () {
console.log(Xml.init(from, to, fromurl)); // <---- this returns undefined why
}
};
xml.js
var Xml = {
to : null,
from : null,
url : null,
init: function (fromaddress, toaddress, link) {
from = fromaddress;
to = toaddress;
url = link;
this.requestXml();
},
requestXml: function () {
$.ajax({
type: "GET",
url: url,
dataType: "xml",
success: this.parseXml
});
},
parseXml: function (xml) {
console.log($(xml)); //<--- works absolutely fine here
return $(xml);
}
};
更新
我也将Xml对象更改为以下内容。在这里,我将xml
转换为string
,然后使用json
插件串入[xmltojson][1]
,但仍会返回undefined。请帮助我如何解决这个问题?
var Xml = {
to : null,
from : null,
url : null,
requestXml: function (fromaddress, toaddress, link) {
from = fromaddress;
to = toaddress;
url = link;
$.ajax({
type: "GET",
url: url,
dataType: "xml",
success: function (xml) {
var xmltostring = Xml.xmlToString(xml),
stringtojson = Xml.stringxmlToJson(xmltostring);
return stringtojson;
}
});
},
xmlToString: function (xmlData) {
var xmlString;
if (window.ActiveXObject) {
xmlString = xmlData.xml;
} else {
xmlString = (new XMLSerializer()).serializeToString(xmlData);
}
return xmlString;
},
stringxmlToJson: function (stringxml) {
return $.xml2json(stringxml);
}
};
答案 0 :(得分:4)
由于AJAX调用(它的异步),您的代码不会返回XML。 相反,你可以触发一个自己的事件并听取它们。
这应该有效:
var Route = {
fromurl : null,
tourl : null,
from : null,
to : null,
init: function (fromaddress, toaddress) {
from = fromaddress;
to = toaddress;
fromurl = 'http://example.com/url=' + fromurl;
tourl = 'http://example.com/url=' + tourl;
Route.searchRoute();
},
searchRoute: function () {
$(document).unbind('your_event').bind('your_event', function (event, xml) {
console.log(xml);
});
Xml.init(from, to, fromurl);
}
};
var Xml = {
to : null,
from : null,
url : null,
init: function (fromaddress, toaddress, link) {
from = fromaddress;
to = toaddress;
url = link;
this.requestXml();
},
requestXml: function () {
$.ajax({
type: "GET",
url: url,
dataType: "xml",
success: this.parseXml
});
},
parseXml: function (xml) {
$(document).trigger('your_event', $(xml));
}
};
答案 1 :(得分:1)
您的init函数不会返回任何内容。这有可能使其发挥作用:
var Xml = {
to : null,
from : null,
url : null,
result : null,
init: function (fromaddress, toaddress, link) {
from = fromaddress;
to = toaddress;
url = link;
this.requestXml();
return this;
},
requestXml: function () {
$.ajax({
type: "GET",
url: url,
dataType: "xml",
success: this.parseXml
});
},
parseXml: function (xml) {
console.log($(xml));
result = $(xml);
},
getResult: function () {
return result;
}
};
然后你可以打电话:
searchRoute: function () {
var xml = Xml.init(from, to, fromurl);
console.log(xml.getResult());
}
<强>解释强>
您的代码尝试从ini()方法获取结果,该方法不返回数据。相反,它调用request-function,它异步调用parseXML() - parseXml()返回的结果永远不会在任何地方结束。那么,这就是你的代码。我的有什么不同?
在我的代码中,init()确实返回了Xml-Class的一个实例。有了这个,parseXml可以将结果保存在变量中(结果在这里)。函数getResult()然后可以返回xml。