Ext.apply(Ext, { /** * Returns the current document body as an {@link Ext.Element}. * @ignore * @memberOf Ext * @return Ext.Element The document body */ getHead : function() { var head; return function() { if (head == undefined) { head = Ext.get(document.getElementsByTagName("head")[0]); } return head; }; }() }); /** * @author Ed Spencer * @class Ext.data.ScriptTagProxy * @extends Ext.data.ServerProxy * *An implementation of Ext.data.DataProxy that reads a data object from a URL which may be in a domain * other than the originating domain of the running page.
* *Note that if you are retrieving data from a page that is in a domain that is NOT the same as the originating domain * of the running page, you must use this class, rather than HttpProxy.
* *The content passed back from a server resource requested by a ScriptTagProxy must be executable JavaScript * source code because it is used as the source inside a <script> tag.
* *In order for the browser to process the returned data, the server must wrap the data object * with a call to a callback function, the name of which is passed as a parameter by the ScriptTagProxy. * Below is a Java example for a servlet which returns data for either a ScriptTagProxy, or an HttpProxy * depending on whether the callback name was passed:
**boolean scriptTag = false; String cb = request.getParameter("callback"); if (cb != null) { scriptTag = true; response.setContentType("text/javascript"); } else { response.setContentType("application/x-json"); } Writer out = response.getWriter(); if (scriptTag) { out.write(cb + "("); } out.print(dataBlock.toJsonString()); if (scriptTag) { out.write(");"); }
Below is a PHP example to do the same thing:
* *$callback = $_REQUEST['callback']; // Create the output object. $output = array('a' => 'Apple', 'b' => 'Banana'); //start output if ($callback) { header('Content-Type: text/javascript'); echo $callback . '(' . json_encode($output) . ');'; } else { header('Content-Type: application/x-json'); echo json_encode($output); }
Below is the ASP.Net code to do the same thing:
** */ Ext.data.ScriptTagProxy = Ext.extend(Ext.data.ServerProxy, { defaultWriterType: 'base', /** * @cfg {String} callbackParam (Optional) The name of the parameter to pass to the server which tells * the server the name of the callback function set up by the load call to process the returned data object. * Defaults to "callback".String jsonString = "{success: true}"; String cb = Request.Params.Get("callback"); String responseString = ""; if (!String.IsNullOrEmpty(cb)) { responseString = cb + "(" + jsonString + ")"; } else { responseString = jsonString; } Response.Write(responseString);
The server-side processing must read this parameter value, and generate * javascript output which calls this named function passing the data object as its only parameter. */ callbackParam : "callback",
/** * @cfg {String} scriptIdPrefix * The prefix string that is used to create a unique ID for the injected script tag element (defaults to 'stcScript') */ scriptIdPrefix: 'stcScript', /** * @cfg {String} callbackPrefix * The prefix string that is used to create a unique callback function name in the global scope. This can optionally * be modified to give control over how the callback string passed to the remote server is generated. Defaults to 'stcCallback' */ callbackPrefix: 'stcCallback', /** * @cfg {String} recordParam * The param name to use when passing records to the server (e.g. 'records=someEncodedRecordString'). * Defaults to 'records' */ recordParam: 'records', /** * Reference to the most recent request made through this Proxy. Used internally to clean up when the Proxy is destroyed * @property lastRequest * @type Ext.data.Request */ lastRequest: undefined, /** * @cfg {Boolean} autoAppendParams True to automatically append the request's params to the generated url. Defaults to true */ autoAppendParams: true, constructor: function(){ this.addEvents( /** * @event exception * Fires when the server returns an exception * @param {Ext.data.Proxy} this * @param {Ext.data.Request} request The request that was sent * @param {Ext.data.Operation} operation The operation that triggered the request */ 'exception' ); Ext.data.ScriptTagProxy.superclass.constructor.apply(this, arguments); }, /** * @private * Performs the read request to the remote domain. ScriptTagProxy does not actually create an Ajax request, * instead we write out a