I have two js files. one is route.js
and second one is xml.js
. In route.js
file I am calling a function which is in xml.js
. What that function does is it parse through xml link and return xml object. When I do console.log($(xml))
inside xml.js then it shows xml but when I return this object and do console.log(Xml.init(param, param, param))
inside route.js
then it returns undefined
. WHY?
Here is my code
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);
}
};
Update
I changed Xml object to following as well. Here I am converting xml
to string
and then string into json
using [xmltojson][1]
plugin but it still returns undefined. Please help me how can I resolve this issue?
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);
}
};