Source: Core/loadText.js

  1. /*global define*/
  2. define([
  3. './loadWithXhr'
  4. ], function(
  5. loadWithXhr) {
  6. 'use strict';
  7. /**
  8. * Asynchronously loads the given URL as text. Returns a promise that will resolve to
  9. * a String once loaded, or reject if the URL failed to load. The data is loaded
  10. * using XMLHttpRequest, which means that in order to make requests to another origin,
  11. * the server must have Cross-Origin Resource Sharing (CORS) headers enabled.
  12. *
  13. * @exports loadText
  14. *
  15. * @param {String|Promise.<String>} url The URL to request, or a promise for the URL.
  16. * @param {Object} [headers] HTTP headers to send with the request.
  17. * @returns {Promise.<String>} a promise that will resolve to the requested data when loaded.
  18. *
  19. *
  20. * @example
  21. * // load text from a URL, setting a custom header
  22. * Cesium.loadText('http://someUrl.com/someJson.txt', {
  23. * 'X-Custom-Header' : 'some value'
  24. * }).then(function(text) {
  25. * // Do something with the text
  26. * }).otherwise(function(error) {
  27. * // an error occurred
  28. * });
  29. *
  30. * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest|XMLHttpRequest}
  31. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  32. * @see {@link http://wiki.commonjs.org/wiki/Promises/A|CommonJS Promises/A}
  33. */
  34. function loadText(url, headers) {
  35. return loadWithXhr({
  36. url : url,
  37. headers : headers
  38. });
  39. }
  40. return loadText;
  41. });