Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timeout in Jquery $.post by emulating $.ajax

Tags:

jquery

How can we emulate the timeout of $.ajax using $.post?

like image 231
newbie Avatar asked Mar 01 '26 13:03

newbie


1 Answers

$.POST is a preset version of $.ajax, so few parameter are already set.

As a matter of fact, a $.post is equal to

$.ajax({
  type: 'POST',
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

But, you can create your own post function to send the request through $.ajax at last.

Here is a custom POST plugin I just coded.

(function( $ ){
  $.myPOST = function( url, data, success, timeout ) {      
    var settings = {
      type : "POST", //predefine request type to POST
      'url'  : url,
      'data' : data,
      'success' : success,
      'timeout' : timeout
    };
    $.ajax(settings)
  };
})( jQuery );

Now the custom POST function is ready

Usage:

$.myPOST(
    "test.php", 
    { 
      'data' : 'value'
    }, 
    function(data) { },
    5000 // this is the timeout   
);

Enjoy :)

like image 161
Starx Avatar answered Mar 03 '26 04:03

Starx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!