7.8.10

Some more words on ScriptTagProxy and Restful Client with ExtJs

I wrote about ScriptTagProxy (STP) before [1]. AFAIK it is the only way to make cross domain calls in ExtJs. So at firt I thougt that it is also the only way to use cross domain Restful Web Services but using a Restful WS with ScriptTagProxy is impossible and here is why;

The working method of STP is simple. Under the hood it creates a script element in the document object [2]. Sets the URI of the script [3] and other stuff. Then it adds it adds it to the head element of the document, so browser get ( literally with GET method ) the script from another domain. Here is the code example that the logic is also used by ExtJs in the doRequest method of STP;
script = document.createElement('script');
  script.src = 'http://example.com/cgi-bin/jsonp?q=What+is+the+meaning+of+life%3F';
  script.id = 'JSONP';
  script.type = 'text/javascript';
  script.charset = 'utf-8';
  // Add the script to the head, upon which it will load and execute.
  var head = document.getElementsByTagName('head')[0];
  head.appendChild(script);

If you know a bit about Restful WS you already saw the problem. After head.appendChild browser GET the script. So there is nothing to do for using POST, PUT and delete method and there is no way to use a cross domain Restful WS with ExtJs. If you think using about XmlHttpRequest and Ajax manully there is also same problem as Patrick Donelan says;
Due to the Same Origin Policy that browsers impose on Javascript, the XmlHttpRequest object is prevented from communicating with external domains (it's an attempted security feature). There are known work-arounds, such as script tag injection (where you use the src attribute on html elements to trick the browser into communicating remotely) however this is limited to GET requests only so you're not going to be able to do much RESTful communication with it. (FYI this is implemented in Ext as the ScriptTagProxy class which is a drop-in for the HttpProxy class).
Your other options are to use Flash for cross-domain requests, or to wait for the W3C Access Control (which lets you make cross-site XmlHttpRequests) to be implemented by all the browsers.

It is posibble to make the request inside your domain to a java bean via JSP. But this is not a good workaround with performance issues also you can't use advantages of Ext.data.Store's restful features and you have to write and maintain so much code.

[1] http://dayonmars.blogspot.com/2010/07/extjs-calls-url-with-options-method.html
[2] You can read it as web page.
[3] Actually the script could be a data package which is surrounded by a functions name. For example; methodName("book": { "id": "1", "author": "Tom Clancy");

Referances;
http://neil.fraser.name/news/2009/07/27/
http://www.codeproject.com/KB/webservices/ExtJS_WebServices_Remote.asp
http://www.sencha.com/learn/Manual:RESTful_Web_Services

Hiç yorum yok:

Yorum Gönder