2014-10-01 6 views
1

방금 ​​jquery 1.11.1로 이동했습니다 (이 프로젝트를 시작하기 전 1.2.x). 엘리먼트에 응답 text/html을 가져 오는 데 사용 된 $ .load 함수는 갑자기 콜백을 발생시키는 것처럼 보이지 않습니다! 실제로로드 할 때 Jquery로드 콜백이 실행되지 않습니다.

$("#thing").load("page", null, callback); 

그래서 내가 그것을 $ 들이게 사용하여 자체에서 그것을 PULS 다음 콜백 실제로의 "완전한"기능에 해고되고, $ 아약스를 사용하는 페이지를 잡고 예상대로 .. JQuery와 코드를 디버깅 $ .ajax. 이 기능은 아약스의 성공 여부에 관계없이 항상 발사됩니다. 잠시 동안 자신을 미친 운전 후, 난 그냥 내 자신이 정말 화가 진행되지 않은 테스트 할 호출을 호출하려고 ...이 내가

$.ajax({url:"page"}) 
      .done(function(r){ 
       $("#thing").html(r); 
      }) 
      .complete(function(){alert("complete");}); 

이이 요소를 채 웁니다하지만 나에게 제공하지 않습니다 않는 찾을 것입니다 alernt .. 그러나 아래는 나에게 경고를 준다! .. 왜?

$.ajax({url:"page"}) 
      .done(function(r){ 
       //$("#thing").html(r); 
       alert("done...?"); 
      }) 
      .complete(function(){alert("complete");}); 

내가 대신 "전체"의 "항상"사용에 대한 코멘트를 들어

UPDATE 1

.. 둘 다 완전한 경고를 얻을. 나는 "부하"없습니다 "아약스를"사용하려는,이 JQ의 코드가 "완료"를 사용하는 방법 1.11.1

// Keep a copy of the old load method 
var _load = jQuery.fn.load; 

/** 
* Load a url into a page 
*/ 
jQuery.fn.load = function(url, params, callback) { 
    if (typeof url !== "string" && _load) { 
     return _load.apply(this, arguments); 
    } 

    var selector, response, type, 
     self = this, 
     off = url.indexOf(" "); 

    if (off >= 0) { 
     selector = jQuery.trim(url.slice(off, url.length)); 
     url = url.slice(0, off); 
    } 

    // If it's a function 
    if (jQuery.isFunction(params)) { 

     // We assume that it's the callback 
     callback = params; 
     params = undefined; 

    // Otherwise, build a param string 
    } else if (params && typeof params === "object") { 
     type = "POST"; 
    } 

    // If we have elements to modify, make the request 
    if (self.length > 0) { 
     jQuery.ajax({ 
      url: url, 

      // if "type" variable is undefined, then "GET" method will be used 
      type: type, 
      dataType: "html", 
      data: params 
     }).done(function(responseText) { 

      // Save response for use in complete callback 
      response = arguments; 

      self.html(selector ? 

       // If a selector was specified, locate the right elements in a dummy div 
       // Exclude scripts to avoid IE 'Permission Denied' errors 
       jQuery("<div>").append(jQuery.parseHTML(responseText)).find(selector) : 

       // Otherwise use the full result 
       responseText); 

     }).complete(callback && function(jqXHR, status) { 
      self.each(callback, response || [ jqXHR.responseText, status, jqXHR ]); 
     }); 
    } 
    return this; 
}; 

예고 ..

+1

'.complete'은 1.11에 존재하지 않으며'.always'입니다. 첫 번째 예는 효과가 있습니다. 'null '을 제거하십시오. –

+0

이것은 제가하는 일이 아닙니다 .. 이것은 실제 jquery "load"메소드가하는 일입니다. 나는 이걸 제어 할 수 없습니다. –

+0

... 귀하의 의견을 이해하지 못합니다. 봐. –

답변

1

문제는 여기에 이상한 하나입니다. 내 페이지에서 다시 오는 HTML이 유효하지 않습니다! 표시 할 수있을만큼 유효합니다. "load"메소드 내에서 $ .parseHTML 메소드는 responseText/html의 append에서 호출됩니다. 이렇게하면 html을 던져서 반환하고 대상 요소에 html이 채워지는 것을 볼 수 있습니다.

무엇을해야할까요?이 예외를 잡아서 무시하므로 "complete"메서드가 매번 호출됩니다 (jquery의 버그로 보입니다).

+0

jquery 약속 시스템 만 기본 메시지와 비슷하면 .catch를 사용할 수 있습니다.> . < –

+0

'.load()'호출에서 반환 된'html'의 오류를 잡아 내야 할 필요가 있습니까? – guest271314

+0

원숭이 패치 $ .parseHTML을 사용하면 발생하는 오류는 무시하고 무시할 수 있습니다. 만약 당신이 잘못된 html을 가지고있다. –

관련 문제