gardenofwine
gardenofwine

Reputation: 1362

Ext.Ajax.request() invokes the failure callback function upon successful request

I'm building a PhoneGap - Sencha-touch application for the iOS and Android platforms. I am loading a local .js file using the Ext.Ajax.request() function.

Funny thing happens - the requests succeeds, but the the 'failure' callback is called. Here is the code:

   Ext.Ajax.request({
       url: 'localfolder/foo.js',
       success : function(xhr){
           // not invoked
       },
       failure : function(response, options){ 
           // response.status == 0
           // wtf, response.responseText contains exactly the contents of the local .js file!
       }
   });

Anyone has an Idea why the 'failure' callback is triggered when in fact the request succedded? [edit] More importantly, how do I make the 'success' callback to be triggered instead?

Upvotes: 4

Views: 13238

Answers (2)

Rid Zeal
Rid Zeal

Reputation: 397

I usually using response like this, maybe it'll help

{
  success:true, // success status
  data: [] // data from process
}

Upvotes: 0

mmigdol
mmigdol

Reputation: 2193

Ext.Ajax simply examines the status code of the underlying XHR (XmlHttpRequest) object it creates. However, it (incorrectly) assumes that the status is an HTTP status. As this Mozilla-provided article discusses, when file: or ftp: schemes are used, a status value of 0 indicates success.

You can modify the onComplete function in Ext.data.Connection (in src/data/Connection.js) to look at the scheme of the URL, and decide if it should use an HTTP-based status or a "0=OK" status to determine success.

It is perfectly legal for non-success results to have a body that can be used by the client. This is why your response.responseText still shows up correctly.

Upvotes: 9

Related Questions