ajax.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* *
  2. * (c) 2010-2017 Christer Vasseng, Torstein Honsi
  3. *
  4. * License: www.highcharts.com/license
  5. */
  6. 'use strict';
  7. import H from '../parts/Globals.js';
  8. /**
  9. * @interface Highcharts.AjaxSettings
  10. *//**
  11. * The URL to call.
  12. *
  13. * @name Highcharts.AjaxSettings#url
  14. * @type {string}
  15. *//**
  16. * The verb to use.
  17. *
  18. * @name Highcharts.AjaxSettings#type
  19. * @type {"get"|"post"|"update"|"delete"}
  20. *//**
  21. * The data type expected.
  22. *
  23. * @name Highcharts.AjaxSettings#dataType
  24. * @type {"json"|"xml"|"text"|"octet"}
  25. *//**
  26. * Function to call on success.
  27. *
  28. * @name Highcharts.AjaxSettings#success
  29. * @type {Function}
  30. *//**
  31. * Function to call on error.
  32. *
  33. * @name Highcharts.AjaxSettings#error
  34. * @type {Function}
  35. *//**
  36. * The payload to send.
  37. *
  38. * @name Highcharts.AjaxSettings#data
  39. * @type {object}
  40. *//**
  41. * The headers; keyed on header name.
  42. *
  43. * @name Highcharts.AjaxSettings#headers
  44. * @type {object}
  45. */
  46. /**
  47. * Perform an Ajax call.
  48. *
  49. * @function Highcharts.ajax
  50. *
  51. * @param {Highcharts.AjaxSettings} attr
  52. * The Ajax settings to use.
  53. */
  54. H.ajax = function (attr) {
  55. var options = H.merge(true, {
  56. url: false,
  57. type: 'GET',
  58. dataType: 'json',
  59. success: false,
  60. error: false,
  61. data: false,
  62. headers: {}
  63. }, attr),
  64. headers = {
  65. json: 'application/json',
  66. xml: 'application/xml',
  67. text: 'text/plain',
  68. octet: 'application/octet-stream'
  69. },
  70. r = new XMLHttpRequest();
  71. function handleError(xhr, err) {
  72. if (options.error) {
  73. options.error(xhr, err);
  74. } else {
  75. // Maybe emit a highcharts error event here
  76. }
  77. }
  78. if (!options.url) {
  79. return false;
  80. }
  81. r.open(options.type.toUpperCase(), options.url, true);
  82. r.setRequestHeader(
  83. 'Content-Type',
  84. headers[options.dataType] || headers.text
  85. );
  86. H.objectEach(options.headers, function (val, key) {
  87. r.setRequestHeader(key, val);
  88. });
  89. r.onreadystatechange = function () {
  90. var res;
  91. if (r.readyState === 4) {
  92. if (r.status === 200) {
  93. res = r.responseText;
  94. if (options.dataType === 'json') {
  95. try {
  96. res = JSON.parse(res);
  97. } catch (e) {
  98. return handleError(r, e);
  99. }
  100. }
  101. return options.success && options.success(res);
  102. }
  103. handleError(r, r.responseText);
  104. }
  105. };
  106. try {
  107. options.data = JSON.stringify(options.data);
  108. } catch (e) {}
  109. r.send(options.data || true);
  110. };