我有兩個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);
}
});
};
當我打印出來的itemList中的Controller.js一切工作正常。 有什麼建議嗎?
我不會建議使用synchronousJAX,但我認爲你需要把'return itemList;'放在你的main函數的底部,而不是在'success'內相關: – Ian
[如何從AJAX返回響應打電話?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) –