如何从异步函数返回响应?

时间:2015-04-12 13:02:24

标签: javascript json function asynchronous callback

我有这个JS代码:

var publicationData = new Array();
var researchers = [];
var year = [];
var title = [];
var pub = [];
var dataJson = [];

callServerAsync();

function callServerAsync(){
    $.get('Year2014.html').then(function(responseData) {
        handleData(responseData);
    });
}
function handleData(responseData){
    var htmlObject = document.createElement('div');
    htmlObject.innerHTML = responseData;
    pub = htmlObject.getElementsByClassName("julkaisu");
    getPublicationData(pub);
    getResearchersYearTitle(publicationData);
    createJson(researchers,year,title);
    console.log(JSON.stringify(dataJson)); //returns dataJson object
}

console.log(JSON.stringify(dataJson)); //returns empty

function createJson(researchers,year,title){

    for(var i = 0; i < researchers.length; i++){
        dataJson.push({ "Researchers": researchers[i],
            "Year": year[i],
            "Title": title[i]
        });
    }

}

我需要访问函数createJson( dataJson )返回的值。

我已阅读此链接(How do I return the response from an asynchronous call?)但仍无法理解如何解决此问题。

我将不胜感激。

0 个答案:

没有答案