For a long time did not write the JS code just encountered such a problem. Most forms in our system do not prevent duplicate submissions.
Because we don't want to handle this on the back end, because if the back-end processing, it is necessary to load the page when a one-time token value, increase the development of the workload does not say, but also easy to forget to do this, at the same time, Ajax is also difficult to deal with, need to submit the failure of the words to return the new token value.
So I want to be in, JS this way. In fact, before and the front mentioned, a long time no movement, had to get a brick thrown out. The idea is, cover off the $.ajax, in which to deal with the problem of preventing duplicate submissions , and front-end business development is not affected, do not change code, no sense.
I think one of the purposes of the framework is to simplify business development, shielding business-related details, so that a line of development peace of mind to write business.
The code is as follows:
/**
* Created by Xiayongsheng on 2016/6/12.
*
/;(function ($) {
var ajax = $.ajax;
Request to store Ajax for
var ajaxstate = {};
var kinhomajax = function () {
var args = Array.prototype.slice.call (arguments, 0);
URL data is consistent,
//should be taken out of the URL, data key values sorted, after the values are spliced together, to SHA1 the value obtained as a fingerprint
//Tired first with the URL as a fingerprint bar
var hash = typeof Args[0] = = = ' string ' Args[0]:args[0].url;
if (typeof Ajaxstate[hash]!== ' undefined ') {
if (Ajaxstate[hash] > 3) {
alert (' Do not repeat commit, request in process ');
}
++ajaxstate[hash];
Return $. Deferred ();
}
Ajaxstate[hash] = 1;
var def = ajax.apply ($,args);
Def.done (function () {
delete ajaxstate[hash];
});
return def;
};
$.ajax = Kinhomajax;
}) (JQuery);
The above is a small set of JS Ajax request to introduce how to prevent the recurrence of the relevant knowledge, hope to help everyone, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!