2015-05-25 80 views
2

我可以宣佈一個jQuery AJAX調用這個方法:聲明jQuery的AJAX調用執行後

var foo = $.ajax({ ... });

但是,這實際上會執行請求,然後有,是否正確?

如何在不初始化的情況下聲明AJAX調用,然後再調用它?例如:

var foo = $.ajax({ ... }); 
// some stuff in between 
foo.execute(); 

謝謝。

編輯 一點信息:我真正想要做的是有一個構建了基於參數AJAX請求的功能,它返回到調用代碼,並有調用代碼管理它的狀態(即能執行它,中止它等)。因此,我並不是簡單地聲明AJAX調用的設置,而是希望獲得$ .ajax返回的實際XHR對象,只能執行它,終止它等。

回答

4

$就回報承諾對象,所以我們可以創建功能:

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)); 
    } 
    }); 
} 

調用此函數,例如:

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

結果寫入控制檯:

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

和執行推遲:

xhr.execute() 
1

您可以將您的請求,然後再執行它

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

,也可以通過設置AJAX,因爲這

jQuery.ajaxSetup({async:false}); 
+0

但是,我們是手頭實際XHR對象?我真正想做的是有一個函數,它根據參數構造一個AJAX請求,將其返回給調用代碼,並讓調用代碼管理其狀態(即能夠執行它,中止它等)。 –

+0

外觀在@Mikalai響應 – Bellash

0

同步運行,如果我得到這個正確的,你想要的東西像

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

但是不會工作,因爲根據定義,ajax調用是被assynchronosly處理的。 你可以使用一個函數來獲取回調,然後將它傳遞給ajax成功處理程序。

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

// some stuff 

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

我不知道如果我真的明白你需要什麼。

編輯:好的,我想我現在明白了!所有你需要做的就是確定你打電話以後的功能...

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

感謝Ninigi,但我想從函數返回XHR對象,並具有執行它的能力並放棄它(在XHR對象中的其他函數中)。上面的Mikalai的回答與我所追求的一致。感謝您的答覆! –

+0

我明白了,你應該接受他的回答,這真的很重要,我必須承認我永遠不會考慮承諾:) – Ninigi

0

嘗試

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>