2013-06-05 2 views
-2

내 자바 프로토 타입 기능은 아래와 같습니다.prototype 함수가 javascript에서 아무 것도 반환하지 않는 이유는 무엇입니까?

ServerDataEngine.prototype.ExecuteCommand = function(command) 
{ 
    try 
    { 
     var result; 
     $.get(command, function (data) { 
      result = jQuery.parseJSON(atob(data)); 
      console.log(result); 
     }); 
     return result; 
    } 
    catch (Exception) 
    { 
     throw (new Exception("Can not connect to server")); 
    } 
} 

그리고 나는이 함수를 이렇게 부릅니다. ExecuteCommand에서

ServerDataEngine.prototype.ExecuteQuery = function (query) 
    { 
    console.log(this.ExecuteCommand(query)); 
    } 

, 다 좋아하지만, ExecuteQuery에, console.log(this.ExecuteCommand(command)) 라인은 undefined을 생산하고 있습니다.

무엇이 문제입니까?

+2

비동기 자바 스크립트의 세계에 오신 것을 환영합니다! * 비동기 작업 결과를 * 반환 할 수없는 곳. 비동기 JS에 대해 연구하고 콜백 *이 반환 대신 사용되는 방법을 시험해보십시오. – Joseph

+0

[AJAX 함수에서 변수가 반환되지 않음] 가능한 복제본 (http://stackoverflow.com/questions/12475269/variable-doesnt-get-returned-from-ajax-function) –

+0

.done() 후 .get() – steo

답변

3

비동기 요청을 동기식으로 처리하고 있습니다. Ajax 호출이 값을 리턴하기 전에 값을 리턴한다. 그런 식으로 일할 수는 없을 것입니다.

+0

해결책이 있습니까? async 추가 : false 매개 변수? –

관련 문제