| An implementation of 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 is executable JavaScript source code that
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(");");
}
Consider the following code :
RecordDef recordDef = new RecordDef(new FieldDef[]{
new StringFieldDef("name", "name"), // "mapping" property not needed if it's the same as "name"
new StringFieldDef("occupation") // this field will use "occupation" as the mapping.
});
JsonReader reader = new JsonReader(new JsonReaderConfig() {
{
setTotalProperty("results"); // The property which contains the total dataset size (optional)
setRoot("rows"); // The property which contains an Array of row objects
setId("id"); // The property within each row object that provides an ID for the record (optional)
}}, recordDef);
If the data is being server from the same domain, then you don;t need to use ScriptTagProxy but instead need to use
com.gwtext.client.data.HttpProxy pointing to the URL that returns data in the following format :
{ 'results': 2, 'rows': [
{ 'id': 1, 'name': 'Bill', occupation: 'Gardener' },
{ 'id': 2, 'name': 'Ben', occupation: 'Horticulturalist' } ]
}
However if this data is being read from another domain, a couple of things need to be done.
First, you need to use ScriptTagProxy pointing to the external url. For example
ScriptTagProxy proxy = new ScriptTagProxy("http://externalurl:8023/foo/bar.php");
Now bar.php cannot return standard Json like the one above when using HttpProxy. Instead it needs to wrap the
Json data in a callback. This is required when reading data from antoher domain. When the ScriptTagProxy tries
for fetch data, it will issue an HTTP request to the specified URL passing the name of the callback function that
it needs the results to be wrapped in. For example http://externalurl:8023/foo/bar.php?start=0&limit=25&sort=nome&dir=DESC&_dc=1196661274168&callback=stcCallback1002
Notice the request paramter "callback" has a value of "stcCallback1002" in the above example. This means that the
request to http://externalurl:8023/foo/bar.php should return Json data wrapped in the callback funtion "stcCallback1002".
stcCallback1002({ 'results': 2, 'rows': [
{ 'id': 1, 'name': 'Bill', occupation: 'Gardener' },
{ 'id': 2, 'name': 'Ben', occupation: 'Horticulturalist' }]);
}
So for ScriptTagProxy to work, the URL that it is reading data from is responsible to read the "callback" paramter,
and wrap the Json data using the name of the passed callback function. By default the name of the callback
request parameter is "callback" but you can specify a different one using the constructor
ScriptTagProxy.ScriptTagProxy(String,int,String) .
|