99热99这里只有精品6国产,亚洲中文字幕在线天天更新,在线观看亚洲精品国产福利片 ,久久久久综合网
Swift
編程
Swift51.com
首頁
社區(qū)
▼
資訊
問答
分享
建議
開源代碼
Xcode下載
Swift教程
hot
登錄
注冊
當(dāng)前位置:
首頁
> 分享
歡迎加入QQ討論群258996829
蘋果6袋
6
麥子學(xué)院
Jquery中如何解決ajax重復(fù)提交問題?
發(fā)布時(shí)間:2016-10-18 19:14 回復(fù):0 查看:3310 最后回復(fù):2016-10-18 19:14
在jquery開發(fā)中,我們常常遇到ajax重復(fù)提交問題,那如何解決ajax的重復(fù)提交呢?本文就將重點(diǎn)和大家分享這部分內(nèi)容,一起來看看吧,希望可以幫助大家更好的
學(xué)習(xí)ajax
。
``` /* * jquery ajax請求過濾,防止ajax請求重復(fù)發(fā)送,對ajax發(fā)送錯(cuò)誤時(shí)進(jìn)行統(tǒng)一處理 / $(function(){ var pendingRequests = {}; // 所有ajax請求的通用前置filter $.ajaxPrefilter(function( options, originalOptions, jqXHR ) { var key = generatePendingRequestKey(options);
//請求是否已經(jīng)存在
if(!pendingRequests[key]){
storePendingRequest(key,jqXHR);
}else{
//如果ajax請求已經(jīng)存在,下一次相同的請求則取消,防止重復(fù)請求
jqXHR.abort();
}
//ajax請求完成時(shí),從臨時(shí)對象中清除請求對應(yīng)的數(shù)據(jù)
var complete = options.complete;
options.complete = function(jqXHR, textStatus) {
//延時(shí)1000毫秒刪除請求信息,表示同Key值請求不能在此時(shí)間段內(nèi)重復(fù)提交
setTimeout(function(){
delete pendingRequests[jqXHR.pendingRequestKey];
},1000);
if ($.isFunction(complete)) {
complete.apply(this, arguments);
}
};
//統(tǒng)一的錯(cuò)誤處理
var error = options.error;
options.error = function(jqXHR, textStatus) {
errorHandler(jqXHR, textStatus);
if ($.isFunction(error)) {
error.apply(this, arguments);
}
};
});
/**
* 當(dāng)ajax請求發(fā)生錯(cuò)誤時(shí),統(tǒng)一進(jìn)行攔截處理的方法
*/
function errorHandler(jqXHR, textStatus){
switch (jqXHR.status){
case(500):
internalError(jqXHR);
break;
case(403):
accessDenied(jqXHR);
break;
case(408):
timeoutError(jqXHR);
break;
case(404):
pageNotFound(jqXHR);
break;
default:
//otherError(jqXHR, textStatus);
}
}
function pageNotFound(jqXHR){
Component.warningMessageBox({
content:"請求訪問的地址或內(nèi)容不存在!"
});
}
function accessDenied(jqXHR){
Component.warningMessageBox({
content:"你無權(quán)進(jìn)行此操作或頁面訪問!"
});
}
function internalError(jqXHR){
Component.warningMessageBox({
content:"服務(wù)器存在錯(cuò)誤,未能正確處理你的請求!"
});
}
function timeoutError(jqXHR){
window.location.href=contextPath + "/j_spring_security_logout";
}
function otherError(jqXHR, textStatus){
Component.warningMessageBox({
content:"未知錯(cuò)誤,錯(cuò)誤代碼:" + textStatus
});
}
/**
* 將ajax請求存儲到臨時(shí)對象中,用于根據(jù)key判斷請求是否已經(jīng)存在
*/
function storePendingRequest(key, jqXHR){
pendingRequests[key] = jqXHR;
jqXHR.pendingRequestKey = key;
}
/**
* 根據(jù)ajax請求參數(shù)構(gòu)建一個(gè)臨時(shí)存儲key,此處簡單的使用url作為key,
* 不考慮為解決請求類型為get時(shí)相同路徑引起的緩存問題,采用隨機(jī)碼構(gòu)建URL的情況
*/
function generatePendingRequestKey(options){
return options.url;
}
});
文章來源:極客頭條
取消引用
您還未登錄,
請先登錄
提 問
熱門帖子
iDev 全平臺開發(fā)者大會門票免費(fèi)送!限量10張!
蘋果Mac Pro垃圾桶 最低配的ME253CH
本人想買個(gè)蘋果電腦搞開發(fā),哪位大俠指點(diǎn)下
求助:failable initializer 'init(name:)' cannot override a non-failable initializer
為慶祝Swift發(fā)布1個(gè)月,雨燕社區(qū)正式上線。
在UITextFeild里輸入數(shù)據(jù),這個(gè)數(shù)據(jù)怎么做加減乘除?
Swift 高仿喜馬拉雅FM
要成為自由職業(yè)者?先要學(xué)會蘋果的Swift哦
用swift實(shí)現(xiàn)的調(diào)用系統(tǒng)相機(jī),相冊的DEMO
關(guān)于嵌入式引用\()
Swift 教程
最新帖子
swift_5.3可以更新了
swift如何實(shí)現(xiàn)左滑刪除
IBM Swift Sandbox訪問
Thread 18: Fatal error: 'try!' expression unexpectedly raised an error: Error
跟隨手勢滑動的ScrollableTextField
Swift5.0什么時(shí)候出
什么時(shí)候出5.0
PerfectTemplate 無法編譯
WWDC19 蘋果宣布全新 UI 框架 SwiftUI
水平滾動視圖Carousel
Xcode 9.4下載
?
Copyright © 2017 Swift 編程 版權(quán)所有
推動 Swift 成為最受歡迎的編程語言!
友鏈、商務(wù)合作:service??swift51.com
手機(jī)版