JQuery overrides the $ .post function

First of all, I apologize for my poor English ... I hope someone understands my question and helps me ...

I am working on a project using many $ .post calls, and I want to improve them by adding the same check for all of them.

I don’t want to change all the scripts one by one, so is there a way to redefine the $ .post function to add the same thing to each of them at the same time? Or maybe a way to call another function on every $ .post? Sort of:

$.post.each(function(){[...]}); 

or

 $('body').on('post', function(){[...]}); 

Thanks!

EDIT: Finally did it the way I wanted! Here is my code:

 // Override $.post (function(){ // Store a reference to the original remove method. var originalPostMethod = jQuery.post; // Define overriding method. jQuery.post = function(data){ // Execute the original method. var callMethod = originalPostMethod.apply( this, arguments); callMethod.success(function(){ //console.log('success'); }); callMethod.error(function(){ //console.log('error'); }); callMethod.complete(function(){ //console.log('complete'); return callMethod; }); } })(); 
+4
source share
2 answers
 $.post = function() { var _method = $.post; // do something you want console.log("do someting"); return _method.apply($, Array.prototype.slice.apply(arguments)); } 
+2
source

You can create a jQuery-Plug that performs validation and then sends the data to the Server. The basics can be found in the jQuery tutorial.

0
source

All Articles