Javascript Return不返回对象

Javascript Return不返回对象,javascript,jquery,samsung-smart-tv,Javascript,Jquery,Samsung Smart Tv,我有两个javascript类(Controller.js和Events.js)。 我从Events.js调用Controller.js中的XML解析器。解析器工作,但不返回任何内容: SceneEvent.prototype.handleKeyDown = function (keyCode) { switch (keyCode) { case sf.key.ENTER: var itemList = null;

我有两个javascript类(Controller.js和Events.js)。 我从Events.js调用Controller.js中的XML解析器。解析器工作,但不返回任何内容:

SceneEvent.prototype.handleKeyDown = function (keyCode) {
    switch (keyCode) {
        case sf.key.ENTER:
            var itemList = null;    
            itemList = Controller.ParseXML("app/data/Event.xml");   
            alert("itemList = " + itemList);
    }
};
Controller.js如下所示:

Controller.ParseXML = function (url) {
    var itemList = null;

    $.ajax({
        type: "GET",
        url: url,
        dataType: "xml",
        async: false,
        success: function(xml) {
            $(xml).find("event").each(function() {
                var _id = $(this).attr("id");
                var _eventItemDay = $(this).find("eventItemDay").text();
                ...
                var _eventItemLocation = $(this).find("eventItemLocation").text();

                itemList = {
                    id: _id,
                    eventItemDay: _eventItemDay,
                    eventItemLocation: _eventItemLocation,
                    ...
                    eventItemLocation: _eventItemLocation
                };
            });
            return itemList;
        },
        error: function(xhr, ajaxOptions, thrownError){
            alert("XML ERROR");
            alert(xhr.status);
            alert(thrownError);
        }
    });
};
当我在Controller.js中打印出itemList时,一切正常。
有什么建议吗?

您必须在
ParseXML
函数末尾返回值,而不是在
success
函数末尾返回值

Controller.ParseXML = function (url) {
    var itemList = null;

    $.ajax({
        type: "GET",
        url: url,
        dataType: "xml",
        async: false,
        success: function(xml) {
            $(xml).find("event").each(function() {
                var _id = $(this).attr("id");
                var _eventItemDay = $(this).find("eventItemDay").text();
                ...
                var _eventItemLocation = $(this).find("eventItemLocation").text();

                itemList = {
                    id: _id,
                    eventItemDay: _eventItemDay,
                    eventItemLocation: _eventItemLocation,
                    ...
                    eventItemLocation: _eventItemLocation
                };
            });

        },
        error: function(xhr, ajaxOptions, thrownError){
            alert("XML ERROR");
            alert(xhr.status);
            alert(thrownError);
        }
    });

    return itemList;
};

您可能想考虑使用Ajax调用异步,并将回调添加到您的PARSISML函数中。事件处理程序中的类似内容:

itemList = Controller.ParseXML("app/data/Event.xml", function(itemList){
    alert("itemList = " + itemList);
});
在ParseXML中:

Controller.ParseXML = function (url, callback) {
var itemList = null;

$.ajax({
    type: "GET",
    url: url,
    dataType: "xml",
    async: true,
    success: function(xml) {
        $(xml).find("event").each(function() {
            var _id = $(this).attr("id");
            var _eventItemDay = $(this).find("eventItemDay").text();
            ...
            var _eventItemLocation = $(this).find("eventItemLocation").text();

            itemList = {
                id: _id,
                eventItemDay: _eventItemDay,
                eventItemLocation: _eventItemLocation,
                ...
                eventItemLocation: _eventItemLocation
            };
            callback( itemList );
        });
    },
    error: function(xhr, ajaxOptions, thrownError){
        alert("XML ERROR");
        alert(xhr.status);
        alert(thrownError);
        callback( "XML ERROR " + xhr.status + " " + thrownError );
    }
});  
})


当然,您需要检查回调中的值是否有错误。

我不建议使用synchronousJAX,但我认为您需要将
return itemList位于主功能的底部,而不是
success
相关的: