jquery.xdr-transport.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * jQuery XDomainRequest Transport Plugin
  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. * https://opensource.org/licenses/MIT
  10. *
  11. * Based on Julian Aubourg's ajaxHooks xdr.js:
  12. * https://github.com/jaubourg/ajaxHooks/
  13. */
  14. /* global define, require, XDomainRequest */
  15. (function (factory) {
  16. 'use strict';
  17. if (typeof define === 'function' && define.amd) {
  18. // Register as an anonymous AMD module:
  19. define(['jquery'], factory);
  20. } else if (typeof exports === 'object') {
  21. // Node/CommonJS:
  22. factory(require('jquery'));
  23. } else {
  24. // Browser globals:
  25. factory(window.jQuery);
  26. }
  27. })(function ($) {
  28. 'use strict';
  29. if (window.XDomainRequest && !$.support.cors) {
  30. $.ajaxTransport(function (s) {
  31. if (s.crossDomain && s.async) {
  32. if (s.timeout) {
  33. s.xdrTimeout = s.timeout;
  34. delete s.timeout;
  35. }
  36. var xdr;
  37. return {
  38. send: function (headers, completeCallback) {
  39. var addParamChar = /\?/.test(s.url) ? '&' : '?';
  40. /**
  41. * Callback wrapper function
  42. *
  43. * @param {number} status HTTP status code
  44. * @param {string} statusText HTTP status text
  45. * @param {object} [responses] Content-type specific responses
  46. * @param {string} [responseHeaders] Response headers string
  47. */
  48. function callback(status, statusText, responses, responseHeaders) {
  49. xdr.onload = xdr.onerror = xdr.ontimeout = $.noop;
  50. xdr = null;
  51. completeCallback(status, statusText, responses, responseHeaders);
  52. }
  53. xdr = new XDomainRequest();
  54. // XDomainRequest only supports GET and POST:
  55. if (s.type === 'DELETE') {
  56. s.url = s.url + addParamChar + '_method=DELETE';
  57. s.type = 'POST';
  58. } else if (s.type === 'PUT') {
  59. s.url = s.url + addParamChar + '_method=PUT';
  60. s.type = 'POST';
  61. } else if (s.type === 'PATCH') {
  62. s.url = s.url + addParamChar + '_method=PATCH';
  63. s.type = 'POST';
  64. }
  65. xdr.open(s.type, s.url);
  66. xdr.onload = function () {
  67. callback(
  68. 200,
  69. 'OK',
  70. { text: xdr.responseText },
  71. 'Content-Type: ' + xdr.contentType
  72. );
  73. };
  74. xdr.onerror = function () {
  75. callback(404, 'Not Found');
  76. };
  77. if (s.xdrTimeout) {
  78. xdr.ontimeout = function () {
  79. callback(0, 'timeout');
  80. };
  81. xdr.timeout = s.xdrTimeout;
  82. }
  83. xdr.send((s.hasContent && s.data) || null);
  84. },
  85. abort: function () {
  86. if (xdr) {
  87. xdr.onerror = $.noop();
  88. xdr.abort();
  89. }
  90. }
  91. };
  92. }
  93. });
  94. }
  95. });