Ext.util.JSONP
Files
Provides functionality to make cross-domain requests with JSONP (JSON with Padding). http://en.wikipedia.org/wiki/JSON#JSONP
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, because of the same origin policy.
The content passed back from a server resource requested by a JSONP requestmust 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 callbackKey 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:
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);
Available since: 1.1.0
Properties
Methods
Make a cross-domain request using JSONP.
Available since: 1.1.0
Parameters
- config : Object
Valid configurations are:
- url - {String} - Url to request data from. (required)
- params - {Object} - A set of key/value pairs to be url encoded and passed as GET parameters in the request.
- callbackKey - {String} - Key specified by the server-side provider.
- callback - {Function} - Will be passed a single argument of the result of the request.
- scope - {Scope} - Scope to execute your callback in.