jquery.xdr-transport.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * jQuery XDomainRequest Transport Plugin 1.1.3
  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. var addParamChar = /\?/.test(s.url) ? '&' : '?';
  38. function callback(status, statusText, responses, responseHeaders) {
  39. xdr.onload = xdr.onerror = xdr.ontimeout = $.noop;
  40. xdr = null;
  41. completeCallback(status, statusText, responses, responseHeaders);
  42. }
  43. xdr = new XDomainRequest();
  44. // XDomainRequest only supports GET and POST:
  45. if (s.type === 'DELETE') {
  46. s.url = s.url + addParamChar + '_method=DELETE';
  47. s.type = 'POST';
  48. } else if (s.type === 'PUT') {
  49. s.url = s.url + addParamChar + '_method=PUT';
  50. s.type = 'POST';
  51. } else if (s.type === 'PATCH') {
  52. s.url = s.url + addParamChar + '_method=PATCH';
  53. s.type = 'POST';
  54. }
  55. xdr.open(s.type, s.url);
  56. xdr.onload = function () {
  57. callback(
  58. 200,
  59. 'OK',
  60. {text: xdr.responseText},
  61. 'Content-Type: ' + xdr.contentType
  62. );
  63. };
  64. xdr.onerror = function () {
  65. callback(404, 'Not Found');
  66. };
  67. if (s.xdrTimeout) {
  68. xdr.ontimeout = function () {
  69. callback(0, 'timeout');
  70. };
  71. xdr.timeout = s.xdrTimeout;
  72. }
  73. xdr.send((s.hasContent && s.data) || null);
  74. },
  75. abort: function () {
  76. if (xdr) {
  77. xdr.onerror = $.noop();
  78. xdr.abort();
  79. }
  80. }
  81. };
  82. }
  83. });
  84. }
  85. }));