jquery.xdr-transport.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * jQuery XDomainRequest Transport Plugin 1.1.2
  3. * https://github.com/blueimp/jQuery-File-Upload
  4. *
  5. * Copyright 2011, Sebastian Tschan
  6. * https://blueimp.net
  7. *
  8. * Licensed under the MIT license:
  9. * http://www.opensource.org/licenses/MIT
  10. *
  11. * Based on Julian Aubourg's ajaxHooks xdr.js:
  12. * https://github.com/jaubourg/ajaxHooks/
  13. */
  14. /*jslint unparam: true */
  15. /*global define, window, XDomainRequest */
  16. (function (factory) {
  17. 'use strict';
  18. if (typeof define === 'function' && define.amd) {
  19. // Register as an anonymous AMD module:
  20. define(['jquery'], factory);
  21. } else {
  22. // Browser globals:
  23. factory(window.jQuery);
  24. }
  25. }(function ($) {
  26. 'use strict';
  27. if (window.XDomainRequest && !$.support.cors) {
  28. $.ajaxTransport(function (s) {
  29. if (s.crossDomain && s.async) {
  30. if (s.timeout) {
  31. s.xdrTimeout = s.timeout;
  32. delete s.timeout;
  33. }
  34. var xdr;
  35. return {
  36. send: function (headers, completeCallback) {
  37. function callback(status, statusText, responses, responseHeaders) {
  38. xdr.onload = xdr.onerror = xdr.ontimeout = $.noop;
  39. xdr = null;
  40. completeCallback(status, statusText, responses, responseHeaders);
  41. }
  42. xdr = new XDomainRequest();
  43. // XDomainRequest only supports GET and POST:
  44. if (s.type === 'DELETE') {
  45. s.url = s.url + (/\?/.test(s.url) ? '&' : '?') +
  46. '_method=DELETE';
  47. s.type = 'POST';
  48. } else if (s.type === 'PUT') {
  49. s.url = s.url + (/\?/.test(s.url) ? '&' : '?') +
  50. '_method=PUT';
  51. s.type = 'POST';
  52. }
  53. xdr.open(s.type, s.url);
  54. xdr.onload = function () {
  55. callback(
  56. 200,
  57. 'OK',
  58. {text: xdr.responseText},
  59. 'Content-Type: ' + xdr.contentType
  60. );
  61. };
  62. xdr.onerror = function () {
  63. callback(404, 'Not Found');
  64. };
  65. if (s.xdrTimeout) {
  66. xdr.ontimeout = function () {
  67. callback(0, 'timeout');
  68. };
  69. xdr.timeout = s.xdrTimeout;
  70. }
  71. xdr.send((s.hasContent && s.data) || null);
  72. },
  73. abort: function () {
  74. if (xdr) {
  75. xdr.onerror = $.noop();
  76. xdr.abort();
  77. }
  78. }
  79. };
  80. }
  81. });
  82. }
  83. }));