xhrWindowNamePlugin.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. define("dojox/io/xhrWindowNamePlugin", [
  2. "dojo/_base/kernel",
  3. "dojo/_base/json",
  4. "dojo/_base/xhr",
  5. "dojox/io/xhrPlugins",
  6. "dojox/io/windowName",
  7. "dojox/io/httpParse",
  8. "dojox/secure/capability"
  9. ], function(dojo, json, xhr, xhrPlugins, windowName, httpParse, capability){
  10. dojo.getObject("io.xhrWindowNamePlugin", true, dojox);
  11. dojox.io.xhrWindowNamePlugin = function(/*String*/url, /*Function?*/httpAdapter, /*Boolean?*/trusted){
  12. // summary:
  13. // Adds the windowName transport as an XHR plugin for the given site. See
  14. // dojox.io.windowName for more information on the transport.
  15. // url:
  16. // Url prefix of the site which can handle windowName requests.
  17. // httpAdapter: This allows for adapting HTTP requests that could not otherwise be
  18. // sent with window.name, so you can use a convention for headers and PUT/DELETE methods.
  19. xhrPlugins.register(
  20. "windowName",
  21. function(method,args){
  22. return args.sync !== true &&
  23. (method == "GET" || method == "POST" || httpAdapter) &&
  24. (args.url.substring(0,url.length) == url);
  25. },
  26. function(method,args,hasBody){
  27. var send = windowName.send;
  28. var load = args.load;
  29. args.load = undefined; //we don't want send to set this callback
  30. var dfd = (httpAdapter ? httpAdapter(send, true) : send)(method, args, hasBody); // use the windowName transport
  31. dfd.addCallback(function(result){
  32. var ioArgs = dfd.ioArgs;
  33. ioArgs.xhr = {
  34. getResponseHeader: function(name){
  35. // convert the hash to an object to act like response headers
  36. return dojo.queryToObject(ioArgs.hash.match(/[^#]*$/)[0])[name];
  37. }
  38. }
  39. // use the XHR content handlers for handling
  40. if(ioArgs.handleAs == 'json'){
  41. // use a secure json verifier, using object capability validator for now
  42. if(!trusted){
  43. capability.validate(result,["Date"],{});
  44. }
  45. return dojo.fromJson(result);
  46. }
  47. return dojo._contentHandlers[ioArgs.handleAs || "text"]({responseText:result});
  48. });
  49. args.load = load;
  50. if(load){
  51. dfd.addCallback(load);
  52. }
  53. return dfd;
  54. }
  55. );
  56. };
  57. return dojox.io.xhrWindowNamePlugin;
  58. });