Hi all:
With a growing number of services allowing the use of XMLHttpRequest2 and its advantages, I found if anything annoying that ST2 didn't provide a convenient way of making use of Ext.Ajax and proxy.Ajax to requests data cross domain.
At first I started using the Ext.Ajax singleton's property Ext.Ajax.useDefaultXhrHeader = false/true, but I hated having to switch its value between calls and besides, using it like this can cause some headaches.
For this reason, I'm sharing now the cleanest solution I've come up with (not the shortest) to make things a bit easier on this subject.
Code:
Ext.define('Ext.data.Cors', {
extend: 'Ext.data.Connection',
alternateClassName: ['Ext.Cors'],
singleton: true,
useDefaultXhrHeader: false,
autoAbort: false
});
Code:
Ext.define('Ext.data.proxy.Cors', {
extend: 'Ext.data.proxy.Ajax',
uses: ['Ext.data.Cors'],
alias: 'proxy.cors',
alternateClassName: ['Ext.data.CorsProxy'],
doRequest: function(operation, callback, scope) {
var writer = this.getWriter(),
request = this.buildRequest(operation, callback, scope);
if (operation.allowWrite()) {
request = writer.write(request);
}
Ext.apply(request, {
headers : this.headers,
timeout : this.timeout,
scope : this,
callback : this.createRequestCallback(request, operation, callback, scope),
method : this.getMethod(request),
disableCaching: false // explicitly set it to false, ServerProxy handles caching
});
Ext.Cors.request(request);
return request;
}
});
Note that for the proxy to work, you must override Ext.data.Model like this:
Code:
Ext.define('YourNamespace.data.Model', {
override: 'Ext.data.Model',
requires: ['Ext.data.proxy.Cors']
});
Since I don't advice you to mess around with ST2 source's at all (updates will break your code if replaced).
I'm not sure if somebody proposed some other solution for CORS requests about the forum, but anyhoo.
Enjoy