2011-04-05 7 views
0

내 앱을 시작할 때 세 개의 Ajax get (Dojo xhrGET) 요청을 보내야하지만 문제는 데이터를 먼저 처리하고 두 번째 데이터를 처리 할 때 세 번째를 보냅니다 (순서가 중요 함). . 나는이 요청을 다른 것의 뒤에 하나 넣었지만 때로는 작동하지 않는다. 동기화 및 해결 방법은 Java에서 잠 그거나 기다릴 수있는 방법이 있습니까?요청에서 데이터를 처리 할 때 ajax 요청을 보내는 방법은 무엇입니까?

답변

1

1.6을 사용하는 경우 새로운 Promises API (dojo.xhrGet에서 반환)를 확인하거나 1.5에서 지연을 사용하십시오. 이를 달성하기위한 '깔끔한'방법을 제공합니다. 기본적으로

당신은 쓸 수 있습니다 :

dojo.xhrGet({ 
    url: './_data/states.json', 
    handleAs: 'json' 
}).then( 
    function(response) { 
     // Response is the XHR response 
     console.log(response); 
     dojo.xhrGet({ 
      url: './_data/'+response.identifier+'.json', 
      handleAs: 'json' 
     }).then(
      function(response2) { 
       // The second XHR will fail 
      }, 
      // Use the error function directly 
      errorFun 
     ) 
    }, 
    function(errResponse) { 
     // Create a function to handle the response 
     errorFun(err); 
    } 
) 

var errorFun = function(err) { 
    console.log(err); 
} 

자세한 내용

에 대한 http://dojotoolkit.org/documentation/tutorials/1.6/deferreds/http://dojotoolkit.org/documentation/tutorials/1.6/promises/를 참조하십시오
1

sync = true 옵션을 사용하고 다른 요청을 뒤에 배치 할 수 있습니다. 이것으로 3 일이 1 일 후에 2 일 후에 보냅니다. 또는로드 기능을 사용하여 1st가 완료된 후 두 번째 요청을 보낼 수 있습니다. 예 : http://dojotoolkit.org/reference-guide/dojo/xhrGet.html

0

우리가 첫 번째 요청의 성공 콜백 메소드의 두 번째 아약스 요청을 만들 수 있습니다 : 자세한 내용은

dojo.xhrGet({ //1st request 
    load: function(){ 
     dojo.xhrGet({ //2nd request 
      load: function(){ 
       dojo.xhrGet({ //3nd request 

       }); 
      } 
     }); 
    } 
}); 

$ 아약스 ({

'type' : 'get', // change if needed 
'dataType' : 'text', // data type you're expecting 
'data' : { 'className' : divClass }, 
'url' : url, 
'success' : function(newClass) { 
    //make the second ajax request... 
} 

});

세 번째 요청에 대해 동일한 작업을 수행하십시오.

관련 문제