/** * @author Ed Spencer * @class Ext.data.AjaxProxy * @extends Ext.data.ServerProxy * *An implementation of {@link Ext.data.Proxy} that processes data requests within the same * domain of the originating page.
* *Note: this class cannot be used to retrieve data from a domain other * than the domain from which the running page was served. For cross-domain requests, use a * {@link Ext.data.ScriptTagProxy ScriptTagProxy}.
* *Be aware that to enable the browser to parse an XML document, the server must set * the Content-Type header in the HTTP response to "text/xml".
* * @constructor * *Note that if this HttpProxy is being used by a {@link Ext.data.Store Store}, then the * Store's call to {@link #load} will override any specified callback and params * options. In this case, use the Store's {@link Ext.data.Store#events events} to modify parameters, * or react to loading events. The Store's {@link Ext.data.Store#baseParams baseParams} may also be * used to pass parameters known at instantiation time.
* *If an options parameter is passed, the singleton {@link Ext.Ajax} object will be used to make * the request.
*/ Ext.data.AjaxProxy = Ext.extend(Ext.data.ServerProxy, { /** * @property actionMethods * Mapping of action name to HTTP request method. In the basic AjaxProxy these are set to 'GET' for 'read' actions and 'POST' * for 'create', 'update' and 'destroy' actions. The {@link Ext.data.RestProxy} maps these to the correct RESTful methods. */ actionMethods: { create : 'POST', read : 'GET', update : 'POST', destroy: 'POST' }, /** * @cfg {Object} headers Any headers to add to the Ajax request. Defaults to undefined. */ constructor: function() { this.addEvents( /** * @event exception * Fires when the server returns an exception * @param {Ext.data.Proxy} this * @param {Object} response The response from the AJAX request * @param {Ext.data.Operation} operation The operation that triggered request */ 'exception' ); Ext.data.AjaxProxy.superclass.constructor.apply(this, arguments); }, /** * @ignore */ 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.Ajax.request(request); return request; }, /** * Returns the HTTP method name for a given request. By default this returns based on a lookup on {@link #actionMethods}. * @param {Ext.data.Request} request The request object * @return {String} The HTTP method to use (should be one of 'GET', 'POST', 'PUT' or 'DELETE') */ getMethod: function(request) { return this.actionMethods[request.action]; }, /** * @private * TODO: This is currently identical to the ScriptTagProxy version except for the return function's signature. There is a lot * of code duplication inside the returned function so we need to find a way to DRY this up. * @param {Ext.data.Request} request The Request object * @param {Ext.data.Operation} operation The Operation being executed * @param {Function} callback The callback function to be called when the request completes. This is usually the callback * passed to doRequest * @param {Object} scope The scope in which to execute the callback function * @return {Function} The callback function */ createRequestCallback: function(request, operation, callback, scope) { var me = this; return function(options, success, response) { if (success === true) { var reader = me.getReader(), result = reader.read(response); //see comment in buildRequest for why we include the response object here Ext.apply(operation, { response : response, resultSet: result }); operation.setCompleted(); operation.setSuccessful(); } else { me.fireEvent('exception', this, response, operation); //TODO: extract error message from reader operation.setException(); } //this callback is the one that was passed to the 'read' or 'write' function above if (typeof callback == 'function') { callback.call(scope || me, operation); } me.afterRequest(request, true); }; } }); Ext.data.ProxyMgr.registerType('ajax', Ext.data.AjaxProxy); //backwards compatibility, remove in Ext JS 5.0 Ext.data.HttpProxy = Ext.data.AjaxProxy;