06a148ff8b40ce1e1e51b9e391e0984cbdeded49.svn-base 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. /* global define, window, 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 {
  21. // Browser globals:
  22. factory(window.jQuery);
  23. }
  24. }(function ($) {
  25. 'use strict';
  26. if (window.XDomainRequest && !$.support.cors) {
  27. $.ajaxTransport(function (s) {
  28. if (s.crossDomain && s.async) {
  29. if (s.timeout) {
  30. s.xdrTimeout = s.timeout;
  31. delete s.timeout;
  32. }
  33. var xdr;
  34. return {
  35. send: function (headers, completeCallback) {
  36. var addParamChar = /\?/.test(s.url) ? '&' : '?';
  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 + addParamChar + '_method=DELETE';
  46. s.type = 'POST';
  47. } else if (s.type === 'PUT') {
  48. s.url = s.url + addParamChar + '_method=PUT';
  49. s.type = 'POST';
  50. } else if (s.type === 'PATCH') {
  51. s.url = s.url + addParamChar + '_method=PATCH';
  52. s.type = 'POST';
  53. }
  54. xdr.open(s.type, s.url);
  55. xdr.onload = function () {
  56. callback(
  57. 200,
  58. 'OK',
  59. {text: xdr.responseText},
  60. 'Content-Type: ' + xdr.contentType
  61. );
  62. };
  63. xdr.onerror = function () {
  64. callback(404, 'Not Found');
  65. };
  66. if (s.xdrTimeout) {
  67. xdr.ontimeout = function () {
  68. callback(0, 'timeout');
  69. };
  70. xdr.timeout = s.xdrTimeout;
  71. }
  72. xdr.send((s.hasContent && s.data) || null);
  73. },
  74. abort: function () {
  75. if (xdr) {
  76. xdr.onerror = $.noop();
  77. xdr.abort();
  78. }
  79. }
  80. };
  81. }
  82. });
  83. }
  84. }));