簡體   English   中英

中止AJAX請求ZeptoJS

[英]Abort AJAX Request ZeptoJS

我想知道是否有辦法.abort()在jQuery中使用.abort()一樣終止ZeptoJS中的AJAX調用。

我已經嘗試過了,但是我一直收到這個錯誤:

Uncaught TypeError: Object #<t> has no method 'abort' 

這就是AJAX調用的樣子:

SITE.Address.pendingZipLookup = $.ajax({
    url: '/us/en-us/services/globalization/getcitysuggestions',
    data: {"ZipCode" : newZipCode},
    type: 'POST',
    success: function (data) {
        if(SITE.Address.suppressZipLookup) return;
        if(data.length === 0) {
            SITE.Navigation.showModal( SITE.TextTranslations.address.noAddressText , SITE.TextTranslations.address.noAddressHeading );
        } else if(data.length == 1) {
            SITE.Address.fillAddressElm($addressFormItem, data[0]);
        } else {
            SITE.Navigation.showModal(  Mustache.to_html( $tmplSelectCity.html(), {Cities:  data} ) , SITE.TextTranslations.address.selectCity );
            $('.modal').find('.modal-header .buttonClose').one('click', clearForm).end();
        }
    },
    error: function() {}
});

我在變量上調用.abort()

if ( SITE.Address.pendingZipLookup !== null && SITE.Address.pendingZipLookup.readyState !== 4 ) {
    console.log(SITE.Address.pendingZipLookup.readyState);
    SITE.Address.pendingZipLookup.abort();
}

.readyState.readyState求值,它只會在控制台中返回undefined

編輯:這是登錄控制台時SITE.Address.pendingZipLookup外觀:

t {_deferred: e}
_deferred: e
_context: Window
_promise: t
_state: "resolved"
_withArguments: Array[3]
always: function (){return n.apply(t,arguments)}
done: function (){return n.apply(t,arguments)}
fail: function (){return n.apply(t,arguments)}
notify: function (){return n.apply(t,arguments)}
notifyWith: function (){return n.apply(t,arguments)}
pipe: function (){return n.apply(t,arguments)}
progress: function (){return n.apply(t,arguments)}
promise: function (){return n.apply(t,arguments)}
reject: function (){return n.apply(t,arguments)}
rejectWith: function (){return n.apply(t,arguments)}
resolve: function (){return n.apply(t,arguments)}
resolveWith: function (){return n.apply(t,arguments)}
then: function (){return n.apply(t,arguments)}
__proto__: e
    always: function (){var t,i,u,f=this;return(t=1<=arguments.length?n.call(arguments,0):[],t.length===0)?this:(i=r(t),this._state==="pending"?(this._alwaysCallbacks||(this._alwaysCallbacks=[]),(u=this._alwaysCallbacks).push.apply(u,i)):i.forEach(function(n){return n.apply(f._context,f._withArguments)}),this)}
    constructor: function e(n){this.then=t(this.then,this),this.resolveWith=t(this.resolveWith,this),this.resolve=t(this.resolve,this),this.rejectWith=t(this.rejectWith,this),this.reject=t(this.reject,this),this.promise=t(this.promise,this),this.progress=t(this.progress,this),this.pipe=t(this.pipe,this),this.notifyWith=t(this.notifyWith,this),this.notify=t(this.notify,this),this.fail=t(this.fail,this),this.done=t(this.done,this),this.always=t(this.always,this),this._state="pending",typeof n=="function"&&n.call(this,this)}
    done: function (){var t,i,u,f=this;return(t=1<=arguments.length?n.call(arguments,0):[],t.length===0)?this:(i=r(t),this._state==="resolved"?i.forEach(function(n){return n.apply(f._context,f._withArguments)}):this._state==="pending"&&(this._doneCallbacks||(this._doneCallbacks=[]),(u=this._doneCallbacks).push.apply(u,i)),this)}
    fail: function (){var t,i,u,f=this;return(t=1<=arguments.length?n.call(arguments,0):[],t.length===0)?this:(i=r(t),this._state==="rejected"?i.forEach(function(n){return n.apply(f._context,f._withArguments)}):this._state==="pending"&&(this._failCallbacks||(this._failCallbacks=[]),(u=this._failCallbacks).push.apply(u,i)),this)}
    notify: function (){var t;return t=1<=arguments.length?n.call(arguments,0):[],this.notifyWith.apply(this,[i].concat(n.call(t))),this}
    notifyWith: function (){var t,i,r;return(i=arguments[0],t=2<=arguments.length?n.call(arguments,1):[],this._state!=="pending")?this:((r=this._progressCallbacks)!=null&&r.forEach(function(n){return n.apply(i,t)}),this)}
    pipe: function (t,i){var r;return r=new e,this.done(function(){var u,i,e;return u=1<=arguments.length?n.call(arguments,0):[],t!=null?(i=t.apply(this,u),f(i)?i.done(function(){var t,i;return t=1<=arguments.length?n.call(arguments,0):[],(i=r.resolveWith).call.apply(i,[r,this].concat(n.call(t)))}).fail(function(){var t,i;return t=1<=arguments.length?n.call(arguments,0):[],(i=r.rejectWith).call.apply(i,[r,this].concat(n.call(t)))}):r.resolveWith.call(r,this,i)):(e=r.resolveWith).call.apply(e,[r,this].concat(n.call(u)))}),this.fail(function(){var t,u,e,o;return t=1<=arguments.length?n.call(arguments,0):[],i!=null?(u=i.apply(this,t),f(u)?u.done(function(){var t,i;return t=1<=arguments.length?n.call(arguments,0):[],(i=r.resolveWith).call.apply(i,[r,this].concat(n.call(t)))}).fail(function(){var t,i;return t=1<=arguments.length?n.call(arguments,0):[],(i=r.rejectWith).call.apply(i,[r,this].concat(n.call(t)))}):r.rejectWith.call(r,this,u),(e=r.rejectWith).call.apply(e,[r,this].concat(n.call(t)))):(o=r.rejectWith).call.apply(o,[r,this].concat(n.call(t)))}),r.promise()}
    progress: function (){var t,i,u;return(t=1<=arguments.length?n.call(arguments,0):[],t.length===0||this._state!=="pending")?this:(i=r(t),this._progressCallbacks||(this._progressCallbacks=[]),(u=this._progressCallbacks).push.apply(u,i),this)}
    promise: function (){return this._promise||(this._promise=new u(this))}
    reject: function (){var t;return t=1<=arguments.length?n.call(arguments,0):[],this.rejectWith.apply(this,[i].concat(n.call(t))),this}
    rejectWith: function (){var t,i,r,u,f=this;return(i=arguments[0],t=2<=arguments.length?n.call(arguments,1):[],this._state!=="pending")?this:(this._state="rejected",this._withArguments=t,this._context=i,(r=this._failCallbacks)!=null&&r.forEach(function(n){return n.apply(f._context,t)}),(u=this._alwaysCallbacks)!=null&&u.forEach(function(n){return n.apply(f._context,t)}),this)}
    resolve: function (){var t;return t=1<=arguments.length?n.call(arguments,0):[],this.resolveWith.apply(this,[i].concat(n.call(t))),this}
    resolveWith: function (){var t,i,r,u,f=this;return(i=arguments[0],t=2<=arguments.length?n.call(arguments,1):[],this._state!=="pending")?this:(this._state="resolved",this._context=i,this._withArguments=t,(r=this._doneCallbacks)!=null&&r.forEach(function(n){return n.apply(f._context,t)}),(u=this._alwaysCallbacks)!=null&&u.forEach(function(n){return n.apply(f._context,t)}),this)}
    state: function (){return this._state}
    then: function (n,t,i){return this.done(n),this.fail(t),this.progress(i),this}
__proto__: Object

如果看起來很奇怪,那是因為我復制了在控制台中獲得的對象,如果有更好的方法來打印結果,則應該讓我知道:)

  • 首先,您需要獲取“請求”實例。 如果使用$.ajax ,則var request = $.ajax(options)
  • 然后,您可以像這樣編寫: request.abort();

如果您無法獲得請求實例,我認為沒有任何方法可以中止請求。

您可以粘貼代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM