2013-04-30 3 views
0

그래서 JSON 데이터를 전달하는 데 문제가 있습니다.JSON 데이터 및 함수

function getFooterContent(){ 
//ajax call to get json file 
$.getJSON("scripts/pageData/footer/footerData.json", function(jsonData){ 
    console.log(jsonData); 
    return jsonData; 
}).fail(function(){ 
    console.log("fail");  
    return -1; 
}); 

//Return the json file 
} 

function someFunction(){ 
    var someContent = new Object(); 
someContent = getFooterContent(); 
console.log(someContent); 
} 

그래서 지금 JSON 파일을 호출하고 있습니다. 그리고 내가 console.log (jsonData)를 쓸 때 Object가 생겼다. 그럼 내가 할 수 있는게있어 .Content. jsonData가 someFunction에 반환되고 console.log (someContent)가 정의되지 않은 경우에도 발생합니다. 나는 이해하지 못한다. 나는 getJSON 함수와 같은 객체라고 생각했다.

답변

2

이것은 $.getJSON이 비동기식이기 때문에 발생합니다. 따라서 getFooterContent()이 반환되면 해당 시점에 JSON 데이터가 아직 검색되지 않았으므로 undefined입니다.

getFooterContent() 대신 promise 개체가 있어야합니다.

function getFooterContent() { 
    var promise = $.getJSON("url"); 
    return promise; 
} 

function someFunction() { 
    var promise = getFooterContent(); 
    var outsideCopy; 

    promise 
    .done(function(data) { 
     outsideCopy = data; 
     console.log(outsideCopy); 
    }) 
    .fail(function(e) { 
     console.log('Error'); 
     console.log(e); 
    }); 
} 

위의 예는 변수 선언을 제거하여 단축 할 수 있습니다. 코드를 쉽게 이해할 수 있도록 코드를 남겨 두었습니다.

+0

하지만 어떻게 접근합니까? 약속의 데이터입니까? – pmac89

+0

데이터는'done' 함수에 전달 된 첫 번째 매개 변수입니다. 'console.log (data);'문을보십시오. – xbonez

+0

데이터를 해당 함수 내에 보관해야합니까? 함수 밖에서 데이터를 사용할 수있는 방법이 없습니까? – pmac89

4

getJSON은 비동기 적으로 호출되므로 예상 한 결과를 얻지 못할 수 있습니다.

설명해 드리죠 :

function getFooterContent(){ 
//ajax call to get json file 
$.getJSON("scripts/pageData/footer/footerData.json", 
    function(jsonData){ // This function is executed when request is ready and server responds with OK 
    console.log(jsonData); 
    return jsonData; 
}). 

fail(function(){ // This when servers responds with error 
    console.log("fail");  
    return -1; 
}); 

//Return the json file 
// You are returning undefined actually 
} 

function someFunction(){ 
    var someContent = new Object(); 
someContent = getFooterContent(); 
console.log(someContent); 
} 

을 당신이 필요합니다

여기
function someFunction(){ 
    var someContent = new Object(); 
    getFooterContent(function(someContent){ 
     // someContent is passed by getFooterContent 
     console.log(someContent); 

    }); 
} 

은 당신이 당신의 함수에 대한 콜백 JavaScript: Passing parameters to a callback function

에 인수를 전달하는 방법이 될 것입니다 :

function getFooterContent(done, fail){ 
    $.getJSON("scripts/pageData/footer/footerData.json", 
    function(jsonData){ // This function is executed when request is ready and server responds with OK 
    // console.log(jsonData); 
    // return jsonData; 
    done.call(null, jsonData); // or done.apply() 
}). 

fail(function(){ // This when servers responds with error 
    // console.log("fail");  
    // return -1; 
    fail.call(); // or fail.apply() 

}); 
} 
+0

그래서 jsonData를 변수에 저장하고 반환하려면 어떻게해야합니까? – pmac89

+0

이 함수 전달 인수를 만들도록 업데이트되었습니다. – juanpastas

+0

@xbonez 응답을 참조하십시오. 역시 매우 좋은 옵션입니다. – juanpastas