다음 링크에서 설명했듯이 jQuery의 $ .ajax 호출은 필요한 경우 나중에 호출을 중단하는 데 사용할 수있는 ajax 객체를 반환합니다.
Abort Ajax requests using jQuery
내 프로젝트는 객체 패턴보다는 아약스 객체를 반환하는 $ .load()를 사용하고 있습니다 ($ (""). 부하()).
$ .load에서 ajax 객체를 얻을 수있는 방법이 있습니까?
다음 링크에서 설명했듯이 jQuery의 $ .ajax 호출은 필요한 경우 나중에 호출을 중단하는 데 사용할 수있는 ajax 객체를 반환합니다.
Abort Ajax requests using jQuery
내 프로젝트는 객체 패턴보다는 아약스 객체를 반환하는 $ .load()를 사용하고 있습니다 ($ (""). 부하()).
$ .load에서 ajax 객체를 얻을 수있는 방법이 있습니까?
현재 API로는 그렇게 할 수 없다고 생각합니다. 그러나, 오픈 소스 덕분에, (jquery-1.6.2.js
에서)을 load
함수의 소스를 살펴 : 그것은 무엇 ajax
전화를 반환하도록
jQuery.fn.extend({
load: function(url, params, callback) {
if (typeof url !== "string" && _load) {
return _load.apply(this, arguments);
// Don't do a request if no elements are being requested
} else if (!this.length) {
return this;
}
var off = url.indexOf(" ");
if (off >= 0) {
var selector = url.slice(off, url.length);
url = url.slice(0, off);
}
// Default to a GET request
var type = "GET";
// If the second parameter was provided
if (params) {
// 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 (typeof params === "object") {
params = jQuery.param(params, jQuery.ajaxSettings.traditional);
type = "POST";
}
}
var self = this;
// Request the remote document
jQuery.ajax({
url: url,
type: type,
dataType: "html",
data: params,
// Complete callback (responseText is used internally)
complete: function(jqXHR, status, responseText) {
// Store the response as specified by the jqXHR object
responseText = jqXHR.responseText;
// If successful, inject the HTML into all the matched elements
if (jqXHR.isResolved()) {
// #4825: Get the actual response in case
// a dataFilter is present in ajaxSettings
jqXHR.done(function(r) {
responseText = r;
});
// See if a selector was specified
self.html(selector ?
// Create a dummy div to hold the results
jQuery("<div>")
// inject the contents of the document in, removing the scripts
// to avoid any 'Permission Denied' errors in IE
.append(responseText.replace(rscript, ""))
// Locate the specified elements
.find(selector) :
// If not, just inject the full result
responseText);
}
if (callback) {
self.each(callback, [ responseText, status, jqXHR ]);
}
}
});
return this;
}
}
약간 위험하지만 가능한 솔루션은 해당 소스를 복사하여 변경하는 것 return this
대신에 반환됩니다. 그것은 향후 버전에서 예고없이 변경 될 수있는 내부 jquery 동작에 의존 할 수 있기 때문에 위험합니다.
아마 아마 loadAjax
당신이 JQuery와의 다른 버전을 사용하는 경우, 거기에서 그것을 가지고, 다른 이름으로 새로운 기능을 jQuery.fn을 확장 할 것입니다.
왜 같은 방법을 사용할 수 있도록 대신 $ .ajax를 사용하도록 변경할 수 없습니까? – momo
나는 어디서나 $ .load()를 사용하는 프로젝트를 가지고있다. $ .load()를 $ .ajax()로 바꾸는 것은 좋은 생각이 아니다. $ .load()가 어떻게 든 요청을 취소하기 위해 jqXHR 객체를 반환 할 수 있다면 좋을 것입니다. – Mumzee