JS Ajax requests How to prevent duplicate submissions _javascript tips

Source: Internet
Author: User

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.