Declare jQuery AJAX call for execution later

大城市里の小女人 提交于 2019-12-06 04:57:37

$.ajax returns promise object, so we can create function:

function prepareAjax(properties) {
  var defer = $.Deferred();

  var promise = defer.promise();

  return $.extend(promise, {
    execute: function () {
      return $.ajax(properties).then(defer.resolve.bind(defer), defer.reject.bind(defer));
    }
  });
}

Call this function, for example:

var xhr = prepareAjax({ method: 'get', url: 'https://localhost:8001' })

Write result to console:

xhr.then(function (result) { console.log(result) });

And execute postponed:

xhr.execute()

You may set up your request and then execute it later

 var ajaxSettings={};
 //....do other things
  $.ajax(ajaxSettings);//then call ajax 

Or may run it synchronously by setting the ajax as this

   jQuery.ajaxSetup({async:false});

If I got this correct, you want something like

var foo = $.ajax({ ... });
// some stuff in between
result = foo.execute(); 
// do something based on result

This, however wont work, because ajax calls are, by definition, being processed assynchronosly. You could use a function taking a callback and pass it to the ajax success handler though.

function myAjaxThingy(callback) {
    $.ajax({...},
    success: function(a){callback.call(a)} )
}

// some stuff

myAjaxThingy(function(a) {// do whatever based on a, or not...})

I am not sure if I really understood what you needed though.

EDIT: Ok, I think I understood now! All you need to do is define a function you call later on...

function myAjaxThingy() {$.ajax({...})}
// something, something, dark side...
myAjaxThingy() // will execute it

Try

var foo = function foo(settings) {
  // if settings passed , extend `$.ajaxSettings`
  this.settings = $.extend({}, $.ajaxSettings, settings);
  this.ajax = $.ajax;
  this.promise = void 0;
  this.execute = function execute() {
    // set `this.promise` to `this.ajax`
    this.promise = this.ajax.call($, this.settings);     
    // return `$.ajax` jQuery promise object
    return this.promise
  };
  // if `this.promise` is defined , call `.abort()`
  // on `this.promise`: `this.ajax` `$.ajax()`
  this.abort = this.promise ? this.promise.abort() : this.promise; 
};

var url = "https://gist.githubusercontent.com/"
          + "guest271314/6a76aa9d2921350c9d53/raw/"
          + "49fbc054731540fa68b565e398d3574fde7366e9/"
          + "abc.txt";

var request1 = new foo({"url":url});

request1.execute()
.then(function(data, textStatus, jqxhr) {
  console.log(data, textStatus, jqxhr)
}, function(jqxhr, textStatus, errorThrown) {
  console.log(textStatus, errorThrown)
});

var request2 = new foo({"url":url});

request2.execute().abort()
.then(function(data, textStatus, jqxhr) {
  console.log(data, textStatus, jqxhr)
}, function(jqxhr, textStatus, errorThrown) {
  console.log(textStatus, errorThrown)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!