0

我有两个 js 文件。一个是route.js,第二个是xml.js。在route.js文件中,我正在调用一个位于xml.js. 该函数的作用是通过 xml 链接解析并返回 xml 对象。当我console.log($(xml))在 xml.js 内部执行时,它会显示 xml,但是当我返回此对象并在console.log(Xml.init(param, param, param))内部执行时route.js,它会返回undefined. 为什么?

这是我的代码

路由.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 对象更改为跟随。在这里,我正在转换xmlstring然后将字符串转换为json使用[xmltojson][1]插件,但它仍然返回未定义。请帮助我如何解决这个问题?

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

2 回答 2

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));
    }
};
于 2012-11-08T08:09:14.010 回答
1

您的 init-function 不返回任何内容。这是使其工作的可能性:

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() 方法中获取结果。相反,它调用请求函数,该函数异步调用 parseXML() - parseXml() 返回的结果永远不会在任何地方结束。所以,这就是你的代码。和我的有什么区别?

在我的代码中,init() 确实返回了 Xml-Class 的一个实例。有了这个, parseXml 可以将结果保存在一个变量中(result here)。然后,函数 getResult() 可以将 xml 返还给您。

于 2012-11-08T08:06:02.947 回答