0

I was wondering if there is way to abort an AJAX call in ZeptoJS the same I would use .abort() in jQuery.

I've tried it but I keep getting this error :

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

This is what the AJAX call looks like :

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

And I'm calling the .abort() on the variable :

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

Neither the .readyState evaluates, it just returns undefined in the console.

EDIT : This is how the SITE.Address.pendingZipLookup looks when logged in the console :

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

If it looks strange it's because I copied the object I got in the console, if there is a better way of printing out the result someone should let me know :)

Roland
  • 9,321
  • 17
  • 79
  • 135

1 Answers1

0
  • First, you need to get the "request" instance. if you use $.ajax, then var request = $.ajax(options).
  • Then you can write like : request.abort();

If you cannot get the request instance, I don't think there is any way to abort the request.

You can paste your code out.

j0k
  • 22,600
  • 28
  • 79
  • 90
Leo Cai
  • 612
  • 1
  • 7
  • 13
  • it seems that SITE.Address.pendingZipLookup == undefined. try this code:if ( SITE.Address.pendingZipLookup != undefined&& SITE.Address.pendingZipLookup != null &&SITE.Address.pendingZipLookup.readyState != 4 ){//todo:} – Leo Cai Jan 08 '13 at 01:31
  • I have added the output of the `SITE.Address.pendingZipLookup` in the question description and it doesn't seem to have any `abort()` methods ... – Roland Jan 08 '13 at 07:45
  • just after the line of SITE.Address.pendingZipLookup = $.ajax({}); try watching the variable in firebug, I'm sure you can see a method named abort(). – Leo Cai Jan 08 '13 at 08:14
  • What do you mean by watch ? I use Chrome's debug tools, can I use those for this purpose ? – Roland Jan 08 '13 at 08:16
  • Chrome is also OK. Add a breakpoint to that line (in sources tab), when your code runs to this breakpoint, watch the variable(simply move mouse over the variable). – Leo Cai Jan 08 '13 at 08:21