通过模拟$ .ajax在Jquery $ .post中超时

new*_*bie 3 jquery

我们如何模拟$.ajax使用的超时$.post

Sta*_*arx 5

$.POST是预设版本$.ajax,因此很少设置参数.

事实上,a $.post等于

$.ajax({
  type: 'POST',
  url: url,
  data: data,
  success: success,
  dataType: dataType
});
Run Code Online (Sandbox Code Playgroud)

但是,您可以创建自己的帖子功能$.ajax,最后发送请求.

这是我刚刚编写的自定义POST插件.

(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 );
Run Code Online (Sandbox Code Playgroud)

现在自定义POST功能已准备就绪

用法:

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

请享用 :)