JSON对象不返回

时间:2016-01-04 04:24:36

标签: javascript json

嗨,我有以下2个功能。如果我在console.log(data)loadJSON,它可以正常工作,但当我在serviceDesc中记录data时,值为undefined。登录loadJSON后数据本身是正确的,但我不知道它为何成为undefined。有人能指出我正确的方向吗?

function serviceDesc(){
    var data = loadJSON('json/services.json',
               function(xhr){console.log(xhr);});
    console.log(data);
}

function loadJSON(path, error){
    var xhr = new XMLHttpRequest();
    if(window.XMLHttpRequest){
        xhr = new XMLHttpRequest();
    } else{
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xhr.onreadystatechange = function(){
        if(xhr.readyState == XMLHttpRequest.DONE){
            if(xhr.status == 200){
                var data = JSON.parse(xhr.responseText);
                return data;
            } else{
                if(error) error(xhr);
            }
        }
    };
    xhr.open("GET", path, true);
    xhr.send();
}

0 个答案:

没有答案
相关问题