過去在使用基於webkit的瀏覽器時遇到此問題。在Web服務器上啓用失敗的請求跟蹤後,我發現該請求甚至沒有從瀏覽器發送。在Web瀏覽器中啓用詳細的調試指出了瀏覽器本身的錯誤。面對瀏覽器中的一個突出的錯誤,我終於到達了以下解決方案。
此代碼將在瀏覽器返回的狀態代碼爲0的情況下重試ajax請求。在所有其他方面,它的功能與jQuery的ajax函數相同。您可以配置代碼將重新嘗試發送請求的次數。
function AjaxRetry(settings, maxTries, interval) {
var self = this;
this.settings = settings;
this.maxTries = typeof maxTries === "number" ? maxTries : 0;
this.completedTries = 0;
this.interval = typeof interval === "number" ? interval : 0;
return tryAjax().promise();
function tryAjax(deferred) {
var d = deferred || $.Deferred();
$.ajax(self.settings)
.done(function (data, textStatus, xhr) {
self.completedTries += 1;
d.resolve(data, status, xhr);
self.done = true;
})
.fail(function (xhr, textStatus, error) {
self.completedTries += 1;
var attemptRetry = false;
// Added in an attempt to handle the ajax errors that appear to be a flaw in the Intermec HTML5 Browser.
// Example error in browser debug log: Curl ERROR: Failed sending data to the peer for url
// This appears to be CURLE_SEND_ERROR (55) - Failed sending network data. https://curl.haxx.se/libcurl/c/libcurl-errors.html
// This code will retry any ajax requests that fail due to the HTML5 browser CURL error
if (typeof (xhr) !== 'undefined' && typeof (textStatus) !== 'undefined' && typeof (error) !== 'undefined') {
if (xhr.readyState === 0 && xhr.responseJSON === undefined && xhr.status === 0 && xhr.statusText === 'error') {
attemptRetry = true;
}
}
if (self.completedTries < self.maxTries && attemptRetry === true) {
//console.log("Waiting " + interval + "ms before retrying...");
setTimeout(function() {
tryAjax(d);
}, self.interval);
} else {
d.reject(xhr, textStatus, error);
}
});
return d;
}
}
請發表你使用,使Ajax請求的相關代碼,你調用服務器代碼。我會說這是開始診斷問題的最好方法。 –