Ajax.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* *
  2. *
  3. * (c) 2010-2021 Christer Vasseng, Torstein Honsi
  4. *
  5. * License: www.highcharts.com/license
  6. *
  7. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  8. *
  9. * */
  10. 'use strict';
  11. import H from '../Core/Globals.js';
  12. import U from '../Core/Utilities.js';
  13. var merge = U.merge, objectEach = U.objectEach;
  14. /**
  15. * @interface Highcharts.AjaxSettingsObject
  16. */ /**
  17. * The payload to send.
  18. *
  19. * @name Highcharts.AjaxSettingsObject#data
  20. * @type {string|Highcharts.Dictionary<any>}
  21. */ /**
  22. * The data type expected.
  23. * @name Highcharts.AjaxSettingsObject#dataType
  24. * @type {"json"|"xml"|"text"|"octet"}
  25. */ /**
  26. * Function to call on error.
  27. * @name Highcharts.AjaxSettingsObject#error
  28. * @type {Function}
  29. */ /**
  30. * The headers; keyed on header name.
  31. * @name Highcharts.AjaxSettingsObject#headers
  32. * @type {Highcharts.Dictionary<string>}
  33. */ /**
  34. * Function to call on success.
  35. * @name Highcharts.AjaxSettingsObject#success
  36. * @type {Function}
  37. */ /**
  38. * The HTTP method to use. For example GET or POST.
  39. * @name Highcharts.AjaxSettingsObject#type
  40. * @type {string}
  41. */ /**
  42. * The URL to call.
  43. * @name Highcharts.AjaxSettingsObject#url
  44. * @type {string}
  45. */
  46. /**
  47. * Perform an Ajax call.
  48. *
  49. * @function Highcharts.ajax
  50. *
  51. * @param {Partial<Highcharts.AjaxSettingsObject>} attr
  52. * The Ajax settings to use.
  53. *
  54. * @return {false|undefined}
  55. * Returns false, if error occured.
  56. */
  57. H.ajax = function (attr) {
  58. var options = merge(true, {
  59. url: false,
  60. type: 'get',
  61. dataType: 'json',
  62. success: false,
  63. error: false,
  64. data: false,
  65. headers: {}
  66. }, attr), headers = {
  67. json: 'application/json',
  68. xml: 'application/xml',
  69. text: 'text/plain',
  70. octet: 'application/octet-stream'
  71. }, r = new XMLHttpRequest();
  72. /**
  73. * @private
  74. * @param {XMLHttpRequest} xhr - Internal request object.
  75. * @param {string|Error} err - Occured error.
  76. * @return {void}
  77. */
  78. function handleError(xhr, err) {
  79. if (options.error) {
  80. options.error(xhr, err);
  81. }
  82. else {
  83. // @todo Maybe emit a highcharts error event here
  84. }
  85. }
  86. if (!options.url) {
  87. return false;
  88. }
  89. r.open(options.type.toUpperCase(), options.url, true);
  90. if (!options.headers['Content-Type']) {
  91. r.setRequestHeader('Content-Type', headers[options.dataType] || headers.text);
  92. }
  93. objectEach(options.headers, function (val, key) {
  94. r.setRequestHeader(key, val);
  95. });
  96. // @todo lacking timeout handling
  97. r.onreadystatechange = function () {
  98. var res;
  99. if (r.readyState === 4) {
  100. if (r.status === 200) {
  101. res = r.responseText;
  102. if (options.dataType === 'json') {
  103. try {
  104. res = JSON.parse(res);
  105. }
  106. catch (e) {
  107. return handleError(r, e);
  108. }
  109. }
  110. return options.success && options.success(res);
  111. }
  112. handleError(r, r.responseText);
  113. }
  114. };
  115. try {
  116. options.data = JSON.stringify(options.data);
  117. }
  118. catch (e) {
  119. // empty
  120. }
  121. r.send(options.data || true);
  122. };
  123. /**
  124. * Get a JSON resource over XHR, also supporting CORS without preflight.
  125. *
  126. * @function Highcharts.getJSON
  127. * @param {string} url
  128. * The URL to load.
  129. * @param {Function} success
  130. * The success callback. For error handling, use the `Highcharts.ajax`
  131. * function instead.
  132. * @return {void}
  133. */
  134. H.getJSON = function (url, success) {
  135. H.ajax({
  136. url: url,
  137. success: success,
  138. dataType: 'json',
  139. headers: {
  140. // Override the Content-Type to avoid preflight problems with CORS
  141. // in the Highcharts demos
  142. 'Content-Type': 'text/plain'
  143. }
  144. });
  145. };
  146. var exports = {
  147. ajax: H.ajax,
  148. getJSON: H.getJSON
  149. };
  150. export default exports;