2014-09-02 3 views
1

JQuery를 사용하고 있습니다. when. 구문은 다음과 같습니다JQuery로 오류 처리시

$.when(
    // Get the HTML 
    $.get("/feature/", function(html) { 
    globalStore.html = html; 
    }), 

    // Get the CSS 
    $.get("/assets/feature.css", function(css) { 
    globalStore.css = css; 
    }), 

    // Get the JS 
    $.getScript("/assets/feature.js") 

).then(function() { 

    // Add CSS to page 
    $("<style />").html(globalStore.css).appendTo("head"); 

    // Add HTML to page 
    $("body").append(globalStore.html); 

}); 

내 질문

  1. 나는 오류 처리를 할 수있는 방법을 때 다른 시나리오에 대한 예외 처리의 서버 결과에 대한 호출 (실패 시나리오) 또는 오류 중 하나?
  2. 여기에서 Ajax 요청을하고 있으므로 Ajax 요청에 대한 시간 초과 기간은 어떻게 정의 할 수 있습니까?
+0

확인하셨습니까? –

답변

2

deferred.then(doneCallbacks, failCallbacks) 당신은 timeout 옵션을 사용할 수 있습니다, 설치에

$.when(
    // Get the HTML 
    $.get("/feature/", function(html) { 
    globalStore.html = html; 
    }), 

    // Get the CSS 
    $.get("/assets/feature.css", function(css) { 
    globalStore.css = css; 
    }), 

    // Get the JS 
    $.getScript("/assets/feature.js") 

).then(function() { 

    // Add CSS to page 
    $("<style />").html(globalStore.css).appendTo("head"); 

    // Add HTML to page 
    $("body").append(globalStore.html); 

}, function(){ 
    //there is an exception in the request 
}); 

처럼 시간 제한을 실패 필터를 취할 수 있습니다.

당신은 세계적으로

jQuery.ajaxSetup({ 
    timeout: 5000 
}) 

같은 중 하나를 사용하거나 timeout 옵션

+0

빠른 답장을 보내 주셔서 감사합니다. 개별 요청에 대해 시간 초과 기간을 적용해야합니다. 'JQuery.ajaxSetup'는이 경우 도움이되지 않습니다. – SharpCoder

+0

@SharpCoder 그런 경우에는 제안 된 두 번째 옵션을 사용할 수 있습니다. –

0

나는 호출이 비동기이기 때문에 그것이 생각과 함께 짧은 버전 $.get() 대신 $.ajax()를 사용할 수 있습니다. 사용하는 경우 :

$.ajax({ 
      url: "file.php", 
      type: "POST", 
      async: false, 
      success: function(data) { 

      } 
    }); 

통화가 동기식입니다.

관련 문제