dom_request_xhr.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. ----------------------------------------------------------
  3. util/Request : 0.1.1 : 2015-03-26
  4. ----------------------------------------------------------
  5. util.request({
  6. url: './dir/something.extension',
  7. data: 'test!',
  8. format: 'text', // text | xml | json | binary
  9. responseType: 'text', // arraybuffer | blob | document | json | text
  10. headers: {},
  11. withCredentials: true, // true | false
  12. ///
  13. onerror: function(evt, percent) {
  14. console.log(evt);
  15. },
  16. onsuccess: function(evt, responseText) {
  17. console.log(responseText);
  18. },
  19. onprogress: function(evt, percent) {
  20. percent = Math.round(percent * 100);
  21. loader.create('thread', 'loading... ', percent);
  22. }
  23. });
  24. */
  25. if (typeof MIDI === 'undefined') MIDI = {};
  26. (function(root) {
  27. var util = root.util || (root.util = {});
  28. util.request = function(opts, onsuccess, onerror, onprogress) { 'use strict';
  29. if (typeof opts === 'string') opts = {url: opts};
  30. ///
  31. var data = opts.data;
  32. var url = opts.url;
  33. var method = opts.method || (opts.data ? 'POST' : 'GET');
  34. var format = opts.format;
  35. var headers = opts.headers;
  36. var responseType = opts.responseType;
  37. var withCredentials = opts.withCredentials || false;
  38. ///
  39. var onsuccess = onsuccess || opts.onsuccess;
  40. var onerror = onerror || opts.onerror;
  41. var onprogress = onprogress || opts.onprogress;
  42. ///
  43. if (typeof NodeFS !== 'undefined' && root.loc.isLocalUrl(url)) {
  44. NodeFS.readFile(url, 'utf8', function(err, res) {
  45. if (err) {
  46. onerror && onerror(err);
  47. } else {
  48. onsuccess && onsuccess({responseText: res});
  49. }
  50. });
  51. return;
  52. }
  53. ///
  54. var xhr = new XMLHttpRequest();
  55. xhr.open(method, url, true);
  56. ///
  57. if (headers) {
  58. for (var type in headers) {
  59. xhr.setRequestHeader(type, headers[type]);
  60. }
  61. } else if (data) { // set the default headers for POST
  62. xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  63. }
  64. if (format === 'binary') { //- default to responseType="blob" when supported
  65. if (xhr.overrideMimeType) {
  66. xhr.overrideMimeType('text/plain; charset=x-user-defined');
  67. }
  68. }
  69. if (responseType) {
  70. xhr.responseType = responseType;
  71. }
  72. if (withCredentials) {
  73. xhr.withCredentials = 'true';
  74. }
  75. if (onerror && 'onerror' in xhr) {
  76. xhr.onerror = onerror;
  77. }
  78. if (onprogress && xhr.upload && 'onprogress' in xhr.upload) {
  79. if (data) {
  80. xhr.upload.onprogress = function(evt) {
  81. onprogress.call(xhr, evt, event.loaded / event.total);
  82. };
  83. } else {
  84. xhr.addEventListener('progress', function(evt) {
  85. var totalBytes = 0;
  86. if (evt.lengthComputable) {
  87. totalBytes = evt.total;
  88. } else if (xhr.totalBytes) {
  89. totalBytes = xhr.totalBytes;
  90. } else {
  91. var rawBytes = parseInt(xhr.getResponseHeader('Content-Length-Raw'));
  92. if (isFinite(rawBytes)) {
  93. xhr.totalBytes = totalBytes = rawBytes;
  94. } else {
  95. return;
  96. }
  97. }
  98. onprogress.call(xhr, evt, evt.loaded / totalBytes);
  99. });
  100. }
  101. }
  102. ///
  103. xhr.onreadystatechange = function(evt) {
  104. if (xhr.readyState === 4) { // The request is complete
  105. if (xhr.status === 200 || // Response OK
  106. xhr.status === 304 || // Not Modified
  107. xhr.status === 308 || // Permanent Redirect
  108. xhr.status === 0 && root.client.cordova // Cordova quirk
  109. ) {
  110. if (onsuccess) {
  111. var res;
  112. if (format === 'xml') {
  113. res = evt.target.responseXML;
  114. } else if (format === 'text') {
  115. res = evt.target.responseText;
  116. } else if (format === 'json') {
  117. try {
  118. res = JSON.parse(evt.target.response);
  119. } catch(err) {
  120. onerror && onerror.call(xhr, evt);
  121. }
  122. }
  123. ///
  124. onsuccess.call(xhr, evt, res);
  125. }
  126. } else {
  127. onerror && onerror.call(xhr, evt);
  128. }
  129. }
  130. };
  131. xhr.send(data);
  132. return xhr;
  133. };
  134. /// NodeJS
  135. if (typeof module !== 'undefined' && module.exports) {
  136. var NodeFS = require('fs');
  137. XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
  138. module.exports = root.util.request;
  139. }
  140. })(MIDI);